2017-04-06 3 views
0

これは簡単な問題のように思えますが、20質問型ゲームを作成するためのクラスが割り当てられています。私は少し余分なことをしてメニューを作ると思った。プログラムは正常に動作しますが、実際のゲームファイルを入力すると、最初の行が出力され、次に最後の行に出力され、出力されます。これは簡単に修正できますか?リンクされたcppファイル - 他のファイルでgetlineを使用することはできません

私のプログラムを起動すると、メニューは必要に応じて実行されます。ユーザー入力がナビゲートするように求めます(1 =ゲームをプレイ、2 =命令、3 =ワードデータベース、4 =終了)。私はゲームを支払うために1を押すと、最初の質問(動物、食べ物、要素、その他)に自分の答えを入力するのではなく、画面をクリアします。または他の?(選択肢を作るためにa、f、e、oを入力します) "と入力して入力したものを入力として使用し、"これはオプションではありません!プログラムを閉じるための入力を促す。これをどうすれば解決できますか?それは数百行の長さであるように私は私のコードのすべてを含むに熱心ではないんだけど、ここに私のメニューファイル、ヘッダファイル、および私のゲームファイルの先頭です:

myheader.h:

#include <windows.h> 
#include <iostream> 

void runGame(); 
void showHowTo(); 
void showWords(); 
void mainMenu(); 

menu.cpp:

#include <iostream> 
#include <string> 
#include "myheader.h" 
#include <windows.h> 

using namespace std; 

int main() 
{ 

    int x = 0; 
    cout << "*******************************\n"; 
    cout << "*****20 QUESTIONS**************\n"; 
    cout << "*******************************\n\n"; 

    cout << "Press a key to make a selection.\n\n\n"; 

    cout << "1: Play Game" << endl; 
    cout << "2: View Instructions" << endl; 
    cout << "3: View Word Database" << endl; 
    cout << "4: Close Window\n\n\n"; 

    cin >> x; 

    if (x == 1) 
    { 
     system("cls"); 
     runGame(); 
    } 
    else if (x == 2) 
    { 
     //instructions function would be here, but was removed as it is not relevant to my question 
    } 
    else if (x == 3) 
    { 
     //word database function would be here, but was removed as it is not relevant to my question 
    } 
    else if (x == 4) 
    { 
     system("pause"); 
     return 0; 
    } 



    system("pause"); 
    return 0; 
} 

game.cpp:

#include <iostream> 
#include <string> 
#include "myheader.h" 
#include <windows.h> 

using namespace std; 

void runGame() 
{ 
    //declaring variables 

    string userInput = ""; 
    bool no = ((userInput == "no") || (userInput == "No") || (userInput == "NO") || (userInput == "nO")); 
    string disappointment = "Awww... Oh well, maybe I'll get it next time!\n"; 
    string excitement = "Yay! I feel smart!\n"; 
    bool animal = ((userInput == "A") || (userInput == "a")); 
    bool food = (userInput == "F" || userInput == "f"); 
    bool element = (userInput == "E" || userInput == "e"); 
    bool other = (userInput == "O" || userInput == "o"); 

    // Prompt user for input (animal, vitamin, element, or other 

    cout << "Welcome to 20 Questions!\nPlease think of something, and answer the questions honestly!\n\n"; 
    cout << "Is your object an animal, vitamin, element, or other? (Please answer A, F, E, or O)\n"; 
    getline(cin,userInput); 
    animal = ((userInput == "A") || (userInput == "a")); 
    food = (userInput == "F" || userInput == "f"); 
    element = (userInput == "E" || userInput == "e"); 
    other = (userInput == "O" || userInput == "o"); 

    if (animal) 
    { 
     //full game in here 
    } 
    else 
    { 
     cout << "That's not an option!\n"; 
    } 

    system("pause"); 
} 

ものは私が苦しんでいるコードファイルです...してくださいそのシステム( "一時停止")が効率的ではないことを指摘していない、私はすでにそれを知っています。私はちょうどゲームが始まるときに私のユーザー入力を働かせる方法を知る必要があります。私の記述がひどい場合、これらは私の出力の写真です。

メニュー:

enter image description here

ゲーム

enter image description here

助けてください!私は明日のためにこれを必要とし、私は立ち往生している!

編集:@ThomasMatthewsに固定された感謝! getline(cin、userInput);を入れて修正しました。もう片方の上に1つ2つ。なぜそれが働いたのか分かりませんが、それはありました!私のプログラムは今は正常に動作します!ありがとうございました!!!!

+0

あなたの出力イメージは私にとっては壊れたリンクです。 – JGroven

+0

@JGrovenを右クリックし、新しいタブで開きます。なぜそれらがうまくいかなかったのか分からない:/ここにあなたがそれを手に入れることができないならば: –

+0

@JGroven [menu](https://ibb.co/g9uttv)[game](https://ibb.co/ huQNmF) –

答えて

1

あなたはCINを行うので>>(Enterキーを押すと改行)STDINが実際にバッファ内の余分な文字を持っているあなたにxは

したがって、あなたのgetline(...)は、実際にすることを入力してキーを取得していますすでにバッファーに入っています。

これを修正するには、cin.ignore();を実行します。あらかじめ、またはcinの代わりにgetline(...)を使用して、>> x

+0

修正済み!ありがとう!答えは他の人よりも丁寧です...ありがとう! –

関連する問題