2016-04-13 20 views
0

Her dönüşün başlangıcında kullanıcı girdisini alan ve girdiye göre farklı şeyler yapması beklenen metin tabanlı bir oyun için bir kod yazdım. Örneğin, "yukarı", oynatıcıyı ızgara üzerinde 1 boşluk yukarı kaldırmalı ve "görünüm" bu alan için belirtilen manzarayı göstermelidir. Geçerli bir giriş olarak belirtilmeyen herhangi bir şey, sistemin bir hata mesajı vermesine neden olur, "Bunu anlamadım." Sorun şu ki: "yukarı doğru aşağı bak" gibi bir şey yazarsam hata mesajını göstermek yerine sırayla her komutu yürütür.std :: cin neden aynı anda birden çok girişi kabul ediyor?

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

[std :: cin.getline() vs std :: cin] (http Olası yinelenen: // stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin) – MikeCAT

+0

yayınladığınız kodunuz hata işleme içermiyor, lütfen hata mesajlarını yazdırmak için yazdığınız parçaları ekleyin. Ve std :: cin aynı anda birden fazla girişi kabul etmez, birbiri ardına bir kelime tüketir, her çağrıda girdi akışından bir kelime okur. Giriş akışında birinden daha fazla kelime olduğunda, sonraki cinse kullanıcı için giriş istemez, sadece bir sonraki kelimeyi alır – Arne

cevap

1

Sen biçimlendirilmiş 'giriş operatörü >> kullanıyor. Temel olarak formatlanmış bir giriş işlemi duracak ve bir beyaz boşluk karakteri gördüğünde geri dönecektir. Böylece girişinizin yukarı aşağı bakışı ile sağdaki görünüm, while döngüsünüz aslında girişteki her komut için altı kez yürütür.

Birden komutları tek satırda, std::getline Kullanmadığınız bir şey olursa yerine:

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