Simple Menu (dejavu)

Ok so I'm creating a simple menu that allows access to other menus and also allows to go back a menu (to the main menu).. I've kept things simple for now, I will be adding alot more options and menus. The problem is my code is all over the place and I'm trying to think of the best way to go about calling functions back and forwards and using loops so that the program doesn't end after a function is called.



Not so Important:

but my default in my switch statement basically flashes once and the program ends which is probably the system("CLS") in the MainMenu function.



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>

using namespace std;

int choice(0); //initialize choice to 0
void MainMenu(); //function prototype
void option1(); //function prototype
void option2(); //function prototype
int main()
{
	
	do //keep menu running until a choice is made
	{
		MainMenu(); //call the MainMenu function
		cin >> choice; //get user input

	switch(choice)
	{
	case 1:
		option1();//call option1 function if option 1 was chosen
		cin >> choice;//get user input for the story menu
		if(choice == 2)
		{
		MainMenu();
		choice = 0;
		}
		else if (choice == 1)
		{
		option2();
		choice = 0;
		}
		if(choice == 1)
		{
		MainMenu();
		choice = 0;
		}
		break;
	case 2:
		exit(0);//exit program choice (2)
		break;

	default:
		cout << "invalid selection, please enter 1 or 2!\n ";
		MainMenu();
		break;
	}
						     //----------------------------------------------------------
	}while(choice == 0);                        // my logic isn't correct, using an invalid number causes the 
						   // default message to flash on screen.
						  //
						 //				ANY SUGGESTIONS? (**dumb moment**)
						//--------------------------------------------------------------



	
system("pause");
return 0;
}


void MainMenu()
{
	system("CLS");
	cout << ":::::::::::::::Main Menu:::::::::::::::\n------------------------------------" <<
	"---" << endl; //Menu Title & Decoration.

	cout << "1) Game Story" << endl;
	//cout << "*) Character Bios" << endl;		  //extra options to add later
	//cout << "*) Character Mechanics" << endl;  //             "
	cout << "2) Exit Program\n\n" << endl;

	cout << "Please enter your selection: ";
}
void option1()
{
	system("CLS");
	cout << ":::::::::::::::Story Menu::::::::::::::\n------------------------------------" <<
	"---" << endl; //Menu Title & Decoration.

	cout << "1) Player 1" << endl;
	cout << "2) Go Back\n\n" << endl;
	cout << "Please enter your selection: ";
}

void option2()
{
	system("CLS");
	cout << ":::::::::::::::Player 1 Story::::::::::::\n------------------------------------" <<
	"---" << endl; //Menu Title & Decoration.

	cout << "\nthis is the story of player 1\n" << endl;
	cout << "Press 1 to return to the main menu or 2 to exit the program" << endl;
}




P.S also option2() doesn't stay on the screen long.... and setting my choice variable to 0 in the switch statements probably didn't help, I'm trying to experiment and see what's going on by myself but I'm at a standstill...
Last edited on
is the problem my choice variable retaining values?
Before I help with your code, I would like you to take a look at a menu program that I created when I was in school, and rebuilt once I had a slightly better knowledge of C++.

The thread that has all of the information: http://www.cplusplus.com/forum/beginner/73054/

Looking at the code, the first few things I notice is that it doesn't look like it's easily maintainable, the organization leaves a little to be desired, and the issues you're having with the menu flashing is that you haven't told the computer to wait for something, it just displays it, clears it, and displays the menu again. Adding a cin.get() might be what you're looking for.
hmm I think I need to keep trying different methods (re-write the code) you're right it's not very organized and maintainable.

I'm still new to c++ even though I've learned the basics there's still a long way to go..

I checked out your menu program, it's pretty neat but a little complex for me to understand 100% at the moment, I can understand bits of it.


I just want a menu that allows back and forward selections without the program ending until stated otherwise.
Last edited on
ok this one is at the skin of my teeth I'm almost there!

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
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;



bool running = true;
int choice(0);
int number(0);
void MainMenu();
int main()
{

	while(running = true)
	{
		MainMenu();
		cin >> choice;
	
		switch(choice)
		{
		case 1:
			system("CLS");
			cout << "welcome to the options menu" << endl;
			cout << "press 1 to go back or 2 to end program: ";
			cin >> number;
			system("CLS");
			if(number == '1')
			{
			running = false;
			break;
			}
			else if(number == '2')
			{
			exit(0);
			break;
			}
			break;
		case 2:
			exit(0);
			break;

		}
	}


system("pause");
return 0;
}

void MainMenu()
{
		cout << "Main Menu"  << endl;
		cout << "1) Options" << endl;
		cout << "2) Exit"    << endl;

		cout << "Please select an option: ";
}



Ok everything works fine, option 2 exits, option 1 takes me to the options menu, then it says press 1 to go back or 2 to exit program, but both 1 and 2 in the options menu take me back to the main menu.

I think the problem is with my else if statement?


edit: to be honest I don't even think it's reading the ifelse statement.

EDIT2:

ok I fixed it

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 <string>
using namespace std;



bool running = true;
int choice(0);
int number(0);
void MainMenu();
int main()
{

	while(running = true)
	{
		MainMenu();
		cin >> choice;
	
		switch(choice)
		{
		case 1:
			system("CLS");
			cout << "welcome to the options menu" << endl;
			cout << "press 1 to go back or 2 to end program: ";
			cin >> number;
			system("CLS");
			if(number == 2) 
			{
			exit(0);
			}
			break;
		case 2:
			exit(0);
			break;

		}
	}


system("pause");
return 0;
}

void MainMenu()
{
		cout << "Main Menu"  << endl;
		cout << "1) Options" << endl;
		cout << "2) Exit"    << endl;

		cout << "Please select an option: ";
}
Last edited on
I'm glad you were able to fix it, however, I believe your organization could still use some work. The idea of using functions is to have something that's easy to find in your code. When I design my programs, my main is typically very lightweight, only calling functions and having a few variables to interact with the different functions.

Regarding your specific program, I would move the options menu to it's own function. Have the menu return a variable, most likely an int, which would be the key pressed, to tell main how to handle it. You seem to be on the right track, but if you'd like to see exactly what I mean, I will gladly show you.
yeah if you wouldn't mind showing me that would be great.
Topic archived. No new replies allowed.