2017-04-06 55 views
0

我知道這可能看起來像一個簡單的問題,但我有一個課堂作業,使20問題類型的遊戲。我想我會多做一點菜單。該程序運行良好,但是當我輸入實際的遊戲文件時,它會輸出第一行然後輸出到我的最後一行並輸出它......它不會讓我鍵入和玩遊戲。這很容易解決嗎?鏈接的cpp文件 - 不能在其他文件中使用getline

當我開始我的程序時,菜單運行正常。它提示用戶輸入導航(1 =玩遊戲,2 =指令,3 =字數據庫,4 =退出)。當我按1支付遊戲時,它會像屏幕一樣清除屏幕,但不是讓我回答第一個問題(動物,食物,元素等),它說:「它是動物,食物,元素,或其他?(輸入a,f,e或o進行選擇)「,就像它應該那樣,但是接着我輸入1並將其用作此問題的輸入,使其輸出」這不是一個選項!「在提示輸入關閉程序之前。我怎樣才能解決這個問題?我並不熱衷於包括我所有的代碼,因爲它是幾百線長,但這裏是我的菜單文件,頭文件,和我的遊戲文件的開頭:

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);兩次一個在另一個之上。不知道它爲什麼起作用,但它確實!我的程序現在運行良好!謝謝!!!!

+0

您的輸出圖像是我的破碎鏈接。 – JGroven

+0

@JGroven右鍵單擊並在新選項卡中打開。不知道爲什麼他們不工作:/他們是,如果你不明白的話: –

+0

@JGroven [菜單](https://ibb.co/g9uttv)[遊戲](https://ibb.co/ huQNmF) –

回答

1

因爲你做CIN >> X你STDIN實際上在緩衝區中一個多餘的字符(當你按下輸入新線)

因此你的函數getline(...)實際上是越來越按Enter鍵已經在緩衝區中。

你可以通過做一個cin.ignore();事先或通過使用getline(...)而不是cin >> x

+0

已修復!謝謝!答案比我得到的其他人更有禮貌......謝謝! –