Best Menu Code?

Jan 1, 2015 at 4:56am
Hello, I was curious as to what would be suggested as the best menu code. It needs to be able to return to the menu if they enter a wrong key. I am making a game on my spare time to better myself with c++ and thought this would be a fun way to go about it. Anyways, I have been trying to mess with a do while loop and have some scrap code for you to look at and tell me if I am going in the correct direction.



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;
int main()
{
	int menu;
	cout << "Created by, --------" << endl << endl
		 << "      {{{{{{{{{{}}}}}}}}}" << endl
		 << "   {{{{{{{{   MENU   }}}}}}}" << endl
		 << "      {{{{{{{{{{}}}}}}}}}" << endl;
		
	do {
		cout << "1.)Start Game " << endl
			 << "2.)End Game " << endl;
			cin >> menu;
			system("pause");

		if (menu == '1') {
			cout << "You have chosen to begin the game!" << endl;
		}
		
	}		
	
	while(menu != '1'); {
		cout << "Invalid Input!" << endl;
		return 0;
	}
	
	
	
	
	
	return 0;
	
	
}


Thanks for the help,
Torm
Last edited on Jan 1, 2015 at 4:56am
Jan 1, 2015 at 5:02am
In my opinion, this is more classic and organized
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
void showMenu()
{
	......
	//this is the menu
	
	return ;
}
//other functions....

int main()
{
	showMenu(); 	//show menu the fist time
	//Enter input
	cin>>choice;
	
	switch(choice)
	{
		case 1:
			beginGame();
			break;
		case 2:
			goToStore();
			break;
		......
		
		case n:
			endGame();
			break;
		default:
			//you have entered wrong menu item
			system("cls");	//clear screen
			showMenu();		//show menu again
	}
	
	system("pause");
	return 0;
}
Jan 1, 2015 at 5:07am
That makes a little sense. I am still roughly learning functions. Also, is it alright to use system("cls"); ? I heard it is not good to use in it programming.

Thanks for the quick reply.
Jan 1, 2015 at 5:10am
I dont think you are at the stage to worry about it.
But if you want to know why its is bad..
http://www.cplusplus.com/articles/j3wTURfi/

I still use it though :)
Last edited on Jan 1, 2015 at 5:12am
Jan 1, 2015 at 5:14am
Alright, thank you for the answer. Would you happen to know how to make such a program in to an actual program. Ex(You click on an icon and it executes) I'm just curious because when I finish I have a few people who would like to try it and I would rather not have to show them all how to get a compiler onto their pc.

Thanks so much though :)
Jan 1, 2015 at 5:20am
If you are talking about GUI and Graphics, I'll say go for Qt.
Its a powerful framework with good documentation.
Jan 1, 2015 at 5:24am
Mmm not so much those over just literally opening this code like it does in the command window and just playing the game.
Jan 1, 2015 at 5:29am
So you simply want an GUI to load your CONSOLE program and play the game in CONSOLE.??
Jan 1, 2015 at 5:29am
Alright, so I have been messing around with that menu you showed me and I am running into a problem with it not looping back and reshowing the menu if you enter something incorrect.

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
44
45
46
47
48
49
50
51
#include <iostream>
#include <cstdlib>
using namespace std;
void showMenu()
{ 
	cout << "1.)Start Game " << endl
		 << "2.)End Game " << endl;
	
}
int beginGame()
{
	cout << "This is the beginning of the game." << endl;
}
int goToStore()
{
	cout << "This is the store." << endl;
}
void endGame()
{
	cout << "Ending Game" << endl;
	
}
int main()
{
	int choice;
	showMenu(); 	//show menu the first time
	//Enter input
	cin>>choice;
	
	switch(choice)
	{
		case 1:
			beginGame();
			break;
		case 2:
			goToStore();
			break;
		
		
		case 0:
			endGame();
			break;
		default:
			//you have entered wrong menu item
			system("cls");	//clear screen
			showMenu();		//show menu again
	}
	
	system("pause");
	return 0;
}
Jan 1, 2015 at 5:37am
Case 1:
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
int main()
{
	int choice;
	
	start:	//start reference
	system("cls");
	showMenu(); 
	cin>>choice;
	
	switch(choice)
	{
		case 1:
			beginGame();
			break;
		case 2:
			goToStore();
			break;
		default:
			cout<<"You entered wrong choice"<<endl;
			system("pause");
			system("cls");
			goto start;
	}
	system("pause");
	return 0;
}

Some people discourage the use of goto

case 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	int choice;
	do
	{
		system("cls");
		showMenu(); 
		cin>>choice;
	
		switch(choice)
		{
			case 1:
				beginGame();
				break;
			case 2:
				goToStore();
			break;
		}
	}while(choice < 0 || choice > 2);
	
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.