case 1:
cout<<"\n1.Regular\n2.Ham and Cheeze\n3.Bacon and Cheeze\n";
break;
case 2:
cout<<"\n1.Cheeze\n2.Sour\n3.BBQ\n";
break;
case 3:
cout<<"\n1.Coke\n2.Sprite\n3.Royal\n";
break;
case 4:
cout<<"Thank you come again!";
break;
default:
cout<<"Please follow the instruction carefuly";
break;
}
getch();
}
Here's my program, my problem is I DON'T KNOW HOW TO REPEAT THE STATEMENT:
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int choice=0;
cout<<"Press\n1(Burger)\n2(Fries)\n3(Drinks)\n4(Exit)\n";
cin>>choice;
bool askAgain = true;
while (askAgain)// As long as askAgain is true, then the loop will work.
{
switch(choice){
case 1:
cout<<"\n1.Regular\n2.Ham and Cheeze\n3.Bacon and Cheeze\n";
break;
case 2:
cout<<"\n1.Cheeze\n2.Sour\n3.BBQ\n";
break;
case 3:
cout<<"\n1.Coke\n2.Sprite\n3.Royal\n";
break;
case 4:
cout<<"Thank you come again!";
askAgain = false;// Here you set askAgain to false to break the loop's condition.
break;
default:
cout<<"Please follow the instruction carefuly";
break;
}
}
getch();
}
I compiled this code successfully. Note i am using <iostream> only. This doesn't however solve the next choice of "regular, ham and cheese, bacon and cheese" etc.. to do that just include another cin and switch for each case.
#include <iostream>
usingnamespace std;
main()
{
int choice=0;
cout<<"Press\n1(Burger)\n2(Fries)\n3(Drinks)\n4(Exit)\n";
cin >> choice;
while (choice != 4) // while choice is not = to 4 (
{
switch(choice)
{
case 1:
cout<<"\n1.Regular\n2.Ham and Cheeze\n3.Bacon and Cheeze\n";
break;
case 2:
cout<<"\n1.Cheeze\n2.Sour\n3.BBQ\n";
break;
case 3:
cout<<"\n1.Coke\n2.Sprite\n3.Royal\n";
break;
default:
cout<<"Please follow the instruction carefuly";
break;
}
cout<<"Press\n1(Burger)\n2(Fries)\n3(Drinks)\n4(Exit)\n";
cin >> choice;
}
return 0;
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int choice=0;
cout<<"Press\n1(Burger)\n2(Fries)\n3(Drinks)\n4(Exit)\n";
int askAgain = 1;
while (askAgain)// As long as askAgain is true, then the loop will work.
{
cin>>choice;
switch(choice){
case 1:
cout<<"\n1.Regular\n2.Ham and Cheeze\n3.Bacon and Cheeze\n";
break;
case 2:
cout<<"\n1.Cheeze\n2.Sour\n3.BBQ\n";
break;
case 3:
cout<<"\n1.Coke\n2.Sprite\n3.Royal\n";
break;
case 4:
cout<<"Thank you come again!";
askAgain = 0;// Here you set askAgain to false to break the loop's condition.
continue;// Skip to the beginning of the loop.
break;
default:
cout<<"Please follow the instruction carefuly";
break;
}
}
getch();
return 0;
}
2. Make the main function return an int; like this
1 2 3 4 5 6
int main ()
{
// Some code...
return 0;
}
3. Don't rely heavily on conio.h since it's on windows platforms only. And if you want your program to wait for a character input, you can declare a char variable and read it at the end of the program like this: char getOut; cin >> getOut;.
char food[2];
int sub1,sub2,sub3,pReg,pCheese;
int pOrd,pBbq,pCheeze;
int pBacon,pCoke,pTea,pPine;
clrscr();
do
{
cout<<"\n\t\t\tMenu:";
cout<<"\n\t\t\tb.Burger";
cout<<"\n\t\t\tf.Fries";
cout<<"\n\t\t\td.Drinks";
cout<<"\n\t\t\te.Exit\n\t\t\t";
cin>>food[2];
switch(food[2])
{
case 'b':
{
cout<<"\n\t\t\t1.Regular\n";
pReg=50;
cout<<"\n\t\t\t2.Cheese\n";
pCheese=60;
cout<<"\n\t\t\t3.Bacon\n\t\t\t";
pBacon=70;
cin>>sub1;
if(sub1==1)
{
cout<<"\n\t\t\tThe total is: "<<pReg;
}
else
if(sub1==2)
{
cout<<"\n\t\t\tThe total is: "<<pCheese;
}
else
{
cout<<"\n\t\t\tThe total is: "<<pBacon;
}
break;
}
case 'f':
{
cout<<"\n\t\t\t1.Cheeze\n";
pCheeze=40;
cout<<"\n\t\t\t2.Barbeque\n";
pBbq=50;
cout<<"\n\t\t\t3.Ordinary\n\t\t\t";
pOrd=30;
cin>>sub2;
if(sub2==1)
{
cout<<"\n\t\t\tThe total is: "<<pCheeze;
}
else
if(sub2==2)
{
cout<<"\n\t\t\tThe total is: "<<pBbq;
}
else
{
cout<<"\n\t\t\tThe total is: "<<pOrd;
}
break;
if(sub3==1)
{
cout<<"\n\t\t\tThe total is: "<<pCoke;
}
else
if(sub3==2)
{
cout<<"\n\t\t\tThe total is: "<<pTea;
}
else
{
cout<<"\n\t\t\tThe total is: "<<pPine;
}
break;
}
case 'e':
{
cout<<"\n\t\t\tThank you come again!.";
break;
}
default:
cout<<"\n\t\t\tInvalid choice, press 'b'/'f'/'d'/'e'only";
}