2016-04-13 12 views
0

Ich habe einen Code für ein textbasiertes Spiel geschrieben, der zu Beginn jeder Runde Benutzereingaben annimmt und verschiedene Dinge basierend auf der Eingabe machen soll. Zum Beispiel sollte "hoch" den Spieler um 1 Feld im Gitter nach oben bewegen, und "look" sollte die spezifizierte Szenerie für dieses Feld anzeigen. Alles, was nicht als gültige Eingabe angegeben ist, bewirkt, dass das System eine Fehlermeldung ausgibt: "Ich habe das nicht verstanden." Das Problem ist: Wenn ich etwas wie "nach oben schaue rechts nach rechts" tippe, wird es jeden Befehl der Reihe nach ausführen, anstatt die Fehlermeldung anzuzeigen.Warum akzeptiert std :: cin mehrere Eingaben gleichzeitig?

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

Mögliche Duplikat [std :: cin.getline() vs. std :: cin] (http: // stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin) – MikeCAT

+0

Ihr Code, den Sie veröffentlicht haben, enthält keine Fehlerbehandlung, fügen Sie bitte die Teile hinzu, die Sie geschrieben haben, um die Fehlermeldungen auszudrucken. Und kein std :: cin akzeptiert nicht mehrere Eingaben gleichzeitig, es verbraucht ein Wort nach dem anderen, liest bei jedem Aufruf ein Wort aus dem Eingabestrom. Wenn der Eingabestream mehr Wörter als eins enthält, fragt das nächste cin den Benutzer nicht nach Eingabe, sondern nimmt nur das nächste Wort – Arne

Antwort

1

Sie verwenden den 'formatiert' Input Operator >>. Und im Grunde stoppt eine formatierte Eingabeoperation und kehrt zurück, wenn sie ein Leerzeichen sieht. Mit Ihrer Eingabe 'nach oben schauen Sie nach rechts', wird Ihre while-Schleife tatsächlich sechs Mal für jeden Befehl in der Eingabe ausgeführt.

Wenn Sie nicht, was mehrere Befehle in einer Zeile, verwenden std::getline statt:

while(getline(cin, input)) { 
    if (input == "quit") break; 
    if (input == "up") ... 
}