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;
}
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:
#include <iostream>
usingnamespace 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;
}
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
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?
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?
#include <iostream>
#include <stdlib.h>
usingnamespace 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;
}
@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 :).
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.