Need help with a C++ Program Write a program that mimics a calculator in a shopp
ID: 640480 • Letter: N
Question
Need help with a C++ Program
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.
Explanation / Answer
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Declare varibles to mainpulate data
string replyItem;
double itemPrice;
double totalAmount = 0;
char ch;
// Setting decimal point for output.
cout << fixed;
cout << setprecision(2);
cout << "Enter: " << endl;
cout << "- (a) to add an item to cart " << endl;
cout << "- (t) running total" << endl;
cout << "- (q) quit" << endl;
cin >> ch;
while (ch != 'q')
{
switch (ch)
{
case 'a':
//Asking for the item name
cout << "Please enter the name of the item : ";
cin >> replyItem;
cout << endl;
//Asking for the item cost
cout << "Please enter the cost of your item : ";
cin >> itemPrice;
//Checking the input
while(cin.fail())
{
cin.clear();
cin.ignore(100, ' ');
cout << "You have inserted an incorrect value. Please try again. :";
cin >> itemPrice;
cout << endl;
}
cout << endl;
totalAmount = totalAmount+ itemPrice;
cout << "Your running total is: " << totalAmount << endl;
break;
case 't':
cout << "You running total is: ";
break;
}
cout << "Enter: " << endl;
cout << "- (a) to add an item to cart " << endl;
cout << "- (t) running total" << endl;
cout << "- (q) quit" << endl;
cin >> ch;
}
return 0;
}