Create C++ containing struct LineItem. Struct represents single line and should
ID: 3620043 • Letter: C
Question
Create C++ containing struct LineItem. Struct represents single line and should declare three fields- price per item, total number of items, and total price. The main() function of the program user enters the price and total number of items. Use arithmetic operators to calculate the total price and output the total price.My program compile however after I input the price and total number of items it knocks me out of the execution screen. The arithmetic is where it stops working properly.
Here is what I have so far:
#include<iostream>
using namespace std;
struct LineItem
{
};
int main ()
{
int TotalNumberItems;
double PricePerItem;
double TotalPrice;
cout <<" Enter the price per item " ;
cin >> PricePerItem;
cout <<" Enter the total number of items " ;
cin >> TotalNumberItems ;
TotalPrice = PricePerItem * TotalNumberItems ;
cout<<"The total price is $" << TotalPrice << endl;
return 0;
}
Explanation / Answer
#include <iostream>#include <iomanip>
using namespace std;
struct LineItem
{
double itemPrice;
double totalPrice;
int numItems;
};
int main()
{
LineItem li;
cout << endl << "Please enter the price of an item: ";
cin >> li.itemPrice;
cout << endl << "Please enter the number of items: ";
cin >> li.numItems;
li.totalPrice = li.itemPrice * li.numItems;
cout << endl << "Total price: $";
cout << fixed << setprecision(2) << li.totalPrice << endl;
return 0;
}