Feb 24, 2013 at 11:21pm UTC
Im new to C++ and programming in general. I want to have the user exit the menu but display the final total. Any help would be appreciated. Thank you
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <iomanip>
using namespace std;
void main()
{
//char choice='Y';
int order = 1;
int num1=0, num2=0, num3=0, num4=0, num5=0;
int num_customers;
int sentinel=0;
const double UnitPrice1= 3.99, UnitPrice2= 4.99,UnitPrice3= 1.99, UnitPrice4= .99;
double AmountofSale1=0, AmountofSale2=0, AmountofSale3=0, AmountofSale4=0;
cout<<"___________________Menu________________\n\n"
<<"_____(1) hamburgers $3.95_____\n"
<<"_____(2) Cheeseburger $4.99_____\n"
<<"_____(3) Fries $1.99_____\n"
<<"_____(4) Cokes $.99_____\n\n";
while (order != sentinel)
{
cout<<"From the list of food, what would you like:\n";
cin>>order;
switch(order)
{
case 0:
break;
case 1:
cout<<"How many Hamburgers would you like to order:\n";
cin>>num1;
AmountofSale1 = UnitPrice1 * num1;
break;
case 2:
cout<<"How many Cheeseburgers would you like to order:\n";
cin>>num2;
AmountofSale2= UnitPrice2 * num2;
break;
case 3:
cout<<"How many Fries would you like to order:\n";
cin>>num3;
AmountofSale3= UnitPrice3 * num3;
break;
case 4:
cout<<"How many Cokes would you like with this order:\n";
cin>>num4;
AmountofSale4= UnitPrice4 * num4;
break;
default: cout<<"Please choose a valid item from our list\n";
}
{
cout<<"You have ordered:\n\n";
cout<<left<<setw(15)<<"ITEM"<<right<<setw(10)<<"QUANTITY"<<right<<setw(15)<<"UNIT PRICE"<<right<<setw(20)<<"AMOUNT OF SALE\n"<<endl;
cout<<"Hamburger" <<setw(15)<<left<< num1 <<setw(9)<<right<< UnitPrice1 <<setw(15) <<right<< AmountofSale1<<endl<<endl;
cout<<"Cheeseburgers"<<setw(15)<<left<< num2 <<setw(5)<<right<< UnitPrice2 <<setw(15) <<right<< AmountofSale2<<endl<<endl;
cout<<"Fries" <<setw(15)<<left<< num3 <<setw(13)<<right<< UnitPrice3 <<setw(15) <<right<< AmountofSale3<<endl<<endl;
cout<<"Cokes" <<setw(15)<<left<< num4 <<setw(13)<<right<< UnitPrice4 <<setw(15) <<right<< AmountofSale4<<endl<<endl;
cout<<setw(50)<<"total "<<(AmountofSale1 + AmountofSale2 + AmountofSale3 + AmountofSale4)<< endl;
}
}
system("PAUSE");
}
Feb 25, 2013 at 12:11am UTC
You could try creating a bool valid_order
. Set it to false to begin with, and then use a while (!valid_order)
loop to ask for user input. In you switch/case list, before breaking, add valid_order = true ;
, and the loop will stop.
Feb 25, 2013 at 12:27am UTC
Ausairman pretty much said I was going to say. Which was you could use a do while. So that it will DO { whatever you want at least once before checking what the while argument is} while ( exitprogram != true)
Feb 25, 2013 at 5:57pm UTC
exit the loop? do
1 2 3
if (condition is met) {
break ;
}
Last edited on Feb 25, 2013 at 5:57pm UTC
Feb 28, 2013 at 2:48am UTC
Sorry what I wrote wasn't that clear, Here is a better example
1 2 3 4 5 6 7 8 9 10 11 12
int main () {
int exit= 0
do {
//Place code here
// use your options for them to choose to quit and just
// use the exit integer
} while ( exit !=0)
}
The way aus was talking about was to use a bool value so it would either be true or false you can do that as well.
Last edited on Feb 28, 2013 at 2:51am UTC