Making a menu

So for my class at school I have to make a menu to display on a kiosk for an amusement park in the future. I'm having trouble with the menu working although I have most of it down. This isn't the first time I did a menu for this class but I have altered it a bit because the other one didn't display an error if the user typed in something that wasn't an option (didn't even think about that being a problem until we talked about it in class today)

Here is my code:
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
42
43
void menu()
{
	int choice;
	cout<<"\t-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"<<endl;
	cout<<"\t /Horacious Glump Ride Interface Menu /"<<endl;
	cout<<"\t-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"<<endl;
	cout<<"\t	1 - Add rider to line"<<endl;
	cout<<"\t	2 - Run the ride"<<endl;
	cout<<"\t	3 - Find wait time"<<endl;
	cout<<"\t	4 - Display the line"<<endl;
	cout<<"\t	0 - Exit program"<<endl;
	cout<<"\t-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"<<endl;
	cout<<"Please enter your selection: ";
	cin>>choice;

	if(choice==1)
	{
		menu();
	}
	else if(choice==2)
	{
		menu();
	}
	else if(choice==3)
	{
		menu();
	}
	else if(choice==4)
	{
		menu();
	}
	else if(choice==0)
	{
		Exit();
	}
	else
	{
		cin.ignore();
		cout<<"** INVALID SELECTION - PLEASE TRY AGAIN **"<<endl;
		menu();
	}
	return;
}


Now for some reason when I try to make the error message display it starts looping it over and over again and it gets stuck doing that infinitely until I close the window myself. Does anyone know what I did wrong?



//Edit:

Also if you could help me out with my placement of cin.ignore and how to use it correctly to dump the answer they gave me that I don't want anymore would be great.
Last edited on
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
if(choice==1)
	{
		menu();
	}
	else if(choice==2)
	{
		menu();
	}
	else if(choice==3)
	{
		menu();
	}
	else if(choice==4)
	{
		menu();
	}
	else if(choice==0)
	{
		Exit();
	}
	else
	{
		cin.ignore();
		cout<<"** INVALID SELECTION - PLEASE TRY AGAIN **"<<endl;
		menu();
	}
	

this will cause unnecessary recursive calls to menu:
instead use this for menus:

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
  while(1)
  {
	   int choice;
	cout<<"\t-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"<<endl;
	cout<<"\t /Horacious Glump Ride Interface Menu /"<<endl;
	cout<<"\t-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"<<endl;
	cout<<"\t	1 - Add rider to line"<<endl;
	cout<<"\t	2 - Run the ride"<<endl;
	cout<<"\t	3 - Find wait time"<<endl;
	cout<<"\t	4 - Display the line"<<endl;
	cout<<"\t	0 - Exit program"<<endl;
	cout<<"\t-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"<<endl;
	cout<<"Please enter your selection: ";
	cin>>choice;

	  if(choice==1)
	{
		//ur functionality for 1 or do nothing itll loop
	}
	else if(choice==2)
	{
		//ur functionality for 2 or do nothing itll loop
	}
	else if(choice==3)
	{
		//ur functionality for 3 or do nothing itll loop
	}
	else if(choice==4)
	{
		//ur functionality for 4 or do nothing itll loop
	}
	else if(choice==0)
	{
		exit(0);
	}
	else
	{
		cout<<"** INVALID SELECTION - PLEASE TRY AGAIN **"<<endl;

	}
  }


best is to use switch case for menus, if they havent covered it for u, explore it.
Last edited on
We were actually just shown how to do those in class recently, I'll look over my notes and I'll see if that will fix my problem. Thanks.
Okay so I was working on it a bit and got this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch(choice)
	{
		case 1:
			// Add Rider Function
			break;
		case 2:
			// Run the ride function
			break;
		case 3:
			// Find wait time function
			break;
		case 4:
			//Display the line function
			break;
		case 0:
			Exit();
			break;
	};


So I can't remember how to display the error message in a switch if someone types something else. Mind showing me?
@BradleyHeat

1
2
default:
cout << "Sorry. Don't understand your request.." << endl;
@whitenite1

Thanks so much, that worked. Looks like my menu is complete! Thanks guys.
Topic archived. No new replies allowed.