Jump to content
  • Advertisement
Sign in to follow this  
flump9

Problems with console text game

This topic is 2552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

[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


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!