I need someone to fix this please!

I am making a menu but whenever I press a number and press enter to open the like "Start Game" is says whats in the start game section but it displays another menu again and again can someone fix that for me? I want it when I press the number to put in like start game is pulls up just a screen and shows the text in start game but there is also a back button that will show me the menu again, if you can help please rewrite the program! Thanks! And sorry about the brackets that make it easyer to read the code also I cant figure out how to do that!

#include <iostream>

using namespace std;

int main()
{
int choice;
bool gameOn = true;
while (gameOn != false){
cout << "*******************************\n";
cout << " 1 - Start the game.\n";
cout << " 2 - Story.\n";
cout << " 3 - Help.\n";
cout << " 4 - Exit.\n";
cout << " Enter your choice and press return: ";

cin >> choice;

switch (choice)
{
case 1:
cout << "game start!\n";
// rest of code here
break;
case 2:
cout << "Story so far....\n";
// rest of code here
break;
case 3:
cout << "Ahahah, you really think I will help you?\n";
// rest of code here
break;
case 4:
cout << "End of Program.\n";
gameOn = false;
break;
default:
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break;
}

}
return 0;
}




please help!
I am no expert, but I wouldn't think you would need the while loop. that just loops it which is why you keep getting the menu. If you were to take the menu out, then you would have an infinite loop with just the switch statement. Here is what I did:

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>

using namespace std;

int main()
{
int choice;
bool gameOn = true;

	cout << "*******************************\n";
	cout << " 1 - Start the game.\n";
	cout << " 2 - Story.\n";
	cout << " 3 - Help.\n";
	cout << " 4 - Exit.\n";
	cout << " Enter your choice and press return: ";
	cin >> choice;

	switch (choice)
	{
		case 1: cout << "game start!\n";
		// You could put your start game code here.  If you want a menu again, just make a "Menu" function and call it on the end
		// of your code.
		break;
		case 2: cout << "Story so far....\n";
		// rest of code here
		break;
		case 3: cout << "Ahahah, you really think I will help you?\n";
		// rest of code here
		break;
		case 4: cout << "End of Program.\n";
				gameOn = false;
		break;
		default: cout << "Not a Valid Choice. \n";
		cout << "Choose again.\n";
		cin >> choice;
		break;
	}


return 0;
}
Last edited on
I can't understand what you're trying to say when you've written:

I want it when I press the number to put in like start game is pulls up just a screen and shows the text in start game but there is also a back button that will show me the menu again


Can you try and improve your English?
What I mean is, when the program that I gave you whenever you would type in like at the top where is takes 1 is start the game and when you type in 1 and press enter it does this

Working on the game
start game
story
help
exit

and it redisplays the menu again, I want it to when I press 1 which equals start game it pulls up a screen with just the game on it or text then I want it to have a back button where I can press back and press another catagory like help or story or exit. Do you get it?
@audioace your code worked perfectly except one I cant press another menu item once I press one, 2 I didnt explain this very well, but can you make a code where it like this


Start Game
Story
Help
Exit

that is the menu that I have but where if I press start game it will pull up a page willout anything else on it and it will say if I press start game,


Working on game code, the there will be a back button that will be like back=1 enter 1 to go back and it will bring me back to all the menu catagorys, can you do that? Do you get what I mean?
I understand. You want to be able to go back to the main menu.
That is about as far as I am going to go unless you really don't understand how to program. We can talk more in private after this for the menu creation, but is this kind of what you wanted to happen with the menu?

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
#include <iostream>

using namespace std;

void menu (int& a)
{
	cout << "*******************************\n";
	cout << " 1 - Start the game.\n";
	cout << " 2 - Story.\n";
	cout << " 3 - Help.\n";
	cout << " 4 - Exit.\n";
	cout << " Enter your choice and press return: ";
	cin >> a;
}

void decision(int& a)
{
	int choice;
	
	switch (a)
	{
		case 1: cout << "To Start A New Game Press 1.\n";
				cout << "To Go Back Press 0.\n";
				cin >> choice;
				if (choice == 0){
					menu(choice);
					decision(choice);
				}
				else if (choice == 1){
					cout << "Here is your game!\n";
				}
				else
					cout << "Invalid Choice";
		break;
		case 2: cout << "Story so far....\n";
		// rest of code here
		break;
		case 3: cout << "Ahahah, you really think I will help you?\n";
		// rest of code here
		break;
		case 4: cout << "End of Program.\n";
				
		break;
		default: cout << "Not a Valid Choice. \n";
		cout << "Choose again.\n";
		cin >> a;
		break;
	}
}

int main()
{
int choice;
bool gameOn = true;

	menu(choice);
	switch (choice)
	{
		case 1: cout << "To Start A New Game Press 1.\n";
				cout << "To Go Back Press 0.\n";
				cin >> choice;
				if (choice == 0){
					menu(choice);
					decision(choice);
				}
				else if (choice == 1){
					cout << "Here is your game!\n";
				}
				else
					cout << "Invalid Choice";
					
		break;
		case 2: cout << "Story so far....\n";
		// rest of code here
		break;
		case 3: cout << "Ahahah, you really think I will help you?\n";
		// rest of code here
		break;
		case 4: cout << "End of Program.\n";
				gameOn = false;
		break;
		default: cout << "Not a Valid Choice. \n";
		cout << "Choose again.\n";
		cin >> choice;
		break;
	}


return 0;
}



Here.

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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    bool gameOn = true;
    while(gameOn!=false)
    {
        int choice;
        cout << "*******************************\n";
        cout << " 1 - Start the game.\n";
        cout << " 2 - Story.\n";
        cout << " 3 - Help.\n";
        cout << " 4 - Exit.\n";
        cout << " Enter your choice and press return: ";
        cin >> choice;
        while(choice==1)
        {
            int ch;
            system("cls");
            cout << "game start!\n";

            // Your Code Here

            cout << "Press 1 to go back to the Main Menu\n";
            cin >> ch;
            if(ch==1)
                break;
        }
        while(choice==2)
        {
            int ch;
            system("cls");
            cout << "Story so far....\n";

            // Your Code Here

            cout << "Press 1 to go back to the Main Menu\n";
            cin >> ch;
            if(ch==1)
                break;
        }
        while(choice==3)
        {
            int ch;
            system("cls");
            cout << "Ahahah, you really think I will help you?\n";

            // Your Code Here

            cout << "Press 1 to go back to the Main Menu\n";
            cin >> ch;
            if(ch==1)
                break;
        }
        while(choice==4)
        {
            system("cls");
            int ch;
            cout << "End of Program.\n";
            gameOn=false;
            break;
        }
    }
    return 0;
}

Last edited on
Nice, @ChosenTorture
@EliteGameIndustries and @audioace

I have made some modifications to the code. Check the code again.
@ChosenTorture, yeah, I was wondering about the true pieces in the while statements, but I have never used system before and this was a much better way than what I was suggesting :).
@audioace: Using system() is not considered good. Check that out here.

http://www.cplusplus.com/articles/4z18T05o/

Using clrscr(); isn't supported because <conio.h> is super deprecated.

The other methods involve using additional libraries which wouldn't be very appropriate to use here in this question. C++ doesn't have a specific method to clear the output screen in a proper manner.
Topic archived. No new replies allowed.