0

I wrote a code for a text-based game that takes user input at the start of every turn and is supposed to do different things based on the input. For instance, "up" should move the player up 1 space on the grid, and "look" should display the specified scenery for that space. Anything that isn't specified as a valid input causes the system to output an error message, "I didn't understand that." The problem is: If I type something like "up look down look right right" it will execute every command in order instead of showing the error message.

 string input = "";
while(input!="quit")
{ 
    cin >> input;
    if (input == "left") {
        if (map[player1.y][player1.x - 1].isWall==true)
            cout << map[player1.y][player1.x - 1].scene;
        else if (player1.x > 0)
            player1.x -= 1;
        else
            cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
    }
    else if (input == "right") {
        if (map[player1.y][player1.x + 1].isWall==true)
            cout << map[player1.y][player1.x + 1].scene << endl;
        else if (player1.x < cols-1)
            player1.x += 1;
        else
            cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
    }
    else if (input == "up") {
        if (map[player1.y - 1][player1.x].isWall==true)
            cout << map[player1.y - 1][player1.x].scene;
        else if (player1.y > 0)
            player1.y -= 1;
        else
            cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
    }
    else if (input == "down") {
        if (map[player1.y + 1][player1.x].isWall==true)
            cout << map[player1.y + 1][player1.x].scene;
        else if (player1.y < rows-1)
            player1.y += 1;
        else
            cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
    }
    else if (input == "look")
        map[player1.y][player1.x].look();
    else if (input == "take")
        player1.pickupCons(map[player1.y][player1.x].potions[0]);
    else
    {
        cout << "I don't understand... type something else!" << endl;
        continue;
    }
LLSv2.0
  • 501
  • 6
  • 20
  • Possible duplicate of [std::cin.getline( ) vs. std::cin](http://stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin) – MikeCAT Apr 13 '16 at 23:33
  • your code that you posted does not contain any error handling, please add the parts that you wrote to print out the error messages. And no std::cin does not accept multiple inputs at the same time, it consumes one word after the other, on each call it reads one word from the input stream. When there are more words on the input stream than one, the next cin will not ask the user for input it will just take the next word – Arne Apr 13 '16 at 23:37

1 Answers1

2

You are using the 'formatted' input operator >>. And basically a formatted input operation will stop and return when it sees a white space character. So with your input 'up look down look right right', your while loop actually executes six times for each command in the input.

If you don't what multiple commands in one line, use std::getline instead:

while(getline(cin, input)) {
    if (input == "quit") break;
    if (input == "up") ...
}
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30