Problems with console text game

Started by
4 comments, last by flump9 12 years, 4 months ago
I am making a text based game and i have this menu where you type in a number and it will say something,but then the menu keeps pooping back up.Any help?And also i need to know how to make the user input their name to change a string so their name is saved in the game.Here is my code:

#include <iostream>
#include <windows.h>
#include <time.h>
#include <stdlib.h>

using namespace std;

void menu()
{
int choice = 0; //Declares the 'Choice' Variable
string charName = "Jack";

while (choice != 3)
{
cout <<"Enter choice:"<< endl <<
"1)Play" << endl << //Prints out Enter Choice ,play,help and Quit
"2)Help" << endl<<
"3)Quit" << endl;

cin >> choice; //Duno

switch(choice) //Switch
{
case 1:
cout << "Let us Begin then," << endl;
break;
case 2:
cout << "The aim of the game is to survive!" << endl;
break;
case 3:
system("exit");
}}}


int main()
{
cout << "Welcome to my RPG" << endl; //Says welcome to Tic Tac Toe
Sleep(1000);
menu();

return 0;
}

Advertisement
First, I wouldn't use System("exit"). It doesn't seem like you really need it. If the user enters 3 you can just return.

The menu keeps popping up because the while condition is still true. For example, say the user enters 1. You print:
Let us Begin then,

and then break out of the switch/case. The while condition will still be true - choice is still not equal to 3, it is equal to 1. Therefore it will print out the menu again and get the user input again.

There are probably different ways to do it, but here is one idea. How about designing a couple of functions.. maybe StartGame() and PrintHelp() which has all of the code you want for playing the game and printing the help menu:

switch(choice) //Switch
{
case 1:
cout << "Let us Begin then," << endl;
StartGame(); //Start the game.
break;
case 2:
cout << "The aim of the game is to survive!" << endl;
PrintHelp(); //Print the help menu.
break;
case 3:
return;
}


To store user input in a string you can just use cin:

string a;
cin >> a;


First, I wouldn't use System("exit"). It doesn't seem like you really need it. If the user enters 3 you can just return.

The menu keeps popping up because the while condition is still true. For example, say the user enters 1. You print:
Let us Begin then,

and then break out of the switch/case. The while condition will still be true - choice is still not equal to 3, it is equal to 1. Therefore it will print out the menu again and get the user input again.

There are probably different ways to do it, but here is one idea. How about designing a couple of functions.. maybe StartGame() and PrintHelp() which has all of the code you want for playing the game and printing the help menu:

switch(choice) //Switch
{
case 1:
cout << "Let us Begin then," << endl;
StartGame(); //Start the game.
break;
case 2:
cout << "The aim of the game is to survive!" << endl;
PrintHelp(); //Print the help menu.
break;
case 3:
return;
}


To store user input in a string you can just use cin:

string a;
cin >> a;




Thanks,that helped me a lot.I have made it so that if you type 1 it will make choice 3,where help will just stay the same as i want it to go back to the menu.

First, I wouldn't use System("exit"). It doesn't seem like you really need it. If the user enters 3 you can just return.

The menu keeps popping up because the while condition is still true. For example, say the user enters 1. You print:
Let us Begin then,

and then break out of the switch/case. The while condition will still be true - choice is still not equal to 3, it is equal to 1. Therefore it will print out the menu again and get the user input again.

There are probably different ways to do it, but here is one idea. How about designing a couple of functions.. maybe StartGame() and PrintHelp() which has all of the code you want for playing the game and printing the help menu:

switch(choice) //Switch
{
case 1:
cout << "Let us Begin then," << endl;
StartGame(); //Start the game.
break;
case 2:
cout << "The aim of the game is to survive!" << endl;
PrintHelp(); //Print the help menu.
break;
case 3:
return;
}


To store user input in a string you can just use cin:

string a;
cin >> a;




BTW how do i use that cin method with user input?what type of code would i write?Like if statements or whatever

BTW how do i use that cin method with user input?what type of code would i write?Like if statements or whatever


I'm not sure I understand what you mean. You just want the user to enter their name once when the program starts? Then you could just include the following code somewhere before the while (or even in main):

string name;
cout << "Enter your name: ";
cin >> name;


You don't need any ifs. You just ask for their name and store it in your string variable.

[quote name='Flump9' timestamp='1324549786' post='4896465']
BTW how do i use that cin method with user input?what type of code would i write?Like if statements or whatever


I'm not sure I understand what you mean. You just want the user to enter their name once when the program starts? Then you could just include the following code somewhere before the while (or even in main):

string name;
cout << "Enter your name: ";
cin >> name;


You don't need any ifs. You just ask for their name and store it in your string variable.
[/quote]

I figured out how to do it.Thanks


This topic is closed to new replies.

Advertisement