C++ Menu Program HELP PLEASE

Can anyone help me in my problem?


#include<iostream.h>
#include<conio.h>

main()
{
clrscr();
int choice=0;

cout<<"Press\n1(Burger)\n2(Fries)\n3(Drinks)\n4(Exit)\n";
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!";
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:

cout<<"Press\n1(Burger)\n2(Fries)\n3(Drinks)\n4(Exit)\n";
cin>>choice;

should i use do while?
Can anyone solve my problem?
yes, you can use a do...while loop to repeat the process as long as the user didn't select '4'
How?

Can you show the code?
This is a slight modification to your code: (Just added a bool variable)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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();
}
Last edited on
@kalgorithmist

There's a error to your code

http://imageshack.us/photo/my-images/508/errorykr.png/

Please help, Its my project in Midterm.
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace 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;
}
Sorry for the late reply. Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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;
}



------------------------------------------------------------------------------------------------------------------------------------------------------------------

Suggestions for better a code:

1. Use
1
2
#include <iostream> 
using namespace std;
instead of
#include <iostream.h> .

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;.
Problem solve with the help of my Instructor

heres the code

#include<iostream.h>
#include<conio.h>

main()

{

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;

}
case 'd':
{
cout<<"\n\t\t\t1.Coke\n";
pCoke=25;
cout<<"\n\t\t\t2.Ice Tea\n";
pTea=30;
cout<<"\n\t\t\t3.Pineapple\n";
pPine=40;

cin>>sub3;

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";
}

}while(food[2]!='e');


getch();
return 0;

}
Topic archived. No new replies allowed.