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