Write a program that mimics a calculator in a shopping cart. Your program must d
ID: 3667767 • Letter: W
Question
Write a program that mimics a calculator in a shopping cart. Your program must display a simple menu to the user with the following choices:
(a)Add an item to the cart.
(t)Running total.
(q)Quit.
If the choice is (a), you must let the user add an item by name (any text with spaces). Then you must ask for the price of the item (read in double), and then display the total so far immediately. That would be the running total. Then display the menu again.
If the choice is (t), give them the total so far, then display the menu again.
If the choice is (q), then give them the total and quit the program.
C++
Explanation / Answer
Please find the required solution:
#include <iostream>
using namespace std;
int main()
{
char input;// variable used for reading an option
bool isInvalid=false,cont=true;//varaible used for controlling loop
char name[20];//varaible used for reading item name
double price,total=0;//variable used for reading price
while(cont)
{//Print menu
cout<<"(a) add total to cart"<<endl;
cout<<"(t) running total"<<endl;
cout<<"(q) quit"<<endl<<"enter option here:";
cin>>input;
//select option base on input
switch(input)
{
case 'a':
cout<<"enter item name:";
cin>>name;
cout<<"enter price of item:";
cin>>price;
total+=price;
case 't':cout<<"Running total:"<<total<<endl;
break;
case 'q':cont=false;
cout<<"you selected to quit"<<endl;
break;
default:isInvalid=true;
}
if(isInvalid)
cout<<"please enter valid option";
}
return 0;
}
Output:
(a) add total to cart (t) running total
(q) quit
enter option here:a
enter item name:mango
enter price of item:12.5
Running total:12.5
(a) add total to cart
(t) running total
(q) quit
enter option here:a
enter item name:guava
enter price of item:25.7
Running total:38.2
(a) add total to cart
(t) running total
(q) quit
enter option here:t
Running total:38.2
(a) add total to cart
(t) running total
(q) quit
enter option here:q
you selected to quit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.