简体   繁体   中英

Why does std::cin accept multiple inputs at the same time?

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

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") ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM