Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problems with console text game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Flump9   Members   -  Reputation: 100

Like
0Likes
Like

Posted 22 December 2011 - 03:05 AM

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


Sponsor:

#2 bbr125   Members   -  Reputation: 109

Like
0Likes
Like

Posted 22 December 2011 - 03:32 AM

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;


#3 Flump9   Members   -  Reputation: 100

Like
0Likes
Like

Posted 22 December 2011 - 03:44 AM

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.

#4 Flump9   Members   -  Reputation: 100

Like
0Likes
Like

Posted 22 December 2011 - 04:29 AM

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

#5 bbr125   Members   -  Reputation: 109

Like
0Likes
Like

Posted 22 December 2011 - 05:25 AM

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.

#6 Flump9   Members   -  Reputation: 100

Like
0Likes
Like

Posted 22 December 2011 - 06:49 AM


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.


I figured out how to do it.Thanks







Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS