链接的 cpp 文件 - 不能在其他文件中使用 getline

Linked cpp files - can't use getline in other file

我知道这似乎是一个简单的问题,但我有一个任务 class 来制作一个 20 题类型的游戏。我想我会做一些额外的事情并制作一个菜单。程序 运行 很好,但是当我输入实际的游戏文件时,它输出第一行然后转到我的最后一行并输出...它不会让我打字和玩游戏。这容易修复吗?

当我启动我的程序时,菜单 运行 是应该的。它提示用户输入导航(1=玩游戏,2=说明,3=单词数据库,4=退出)。当我按 1 支付游戏费用时,它会像它应该的那样清除屏幕,但它不会让我输入我对第一个问题(动物、食物、元素、其他)的回答,而是说 "Is it animal, food, element, or other? (Enter a, f, e, or o to make a selection)",就像它一样应该,但随后将我输入的 1 用作此问题的输入,使其在提示输入以关闭程序之前输出 "That's not an option!"。我怎样才能解决这个问题?我不想包含我的所有代码,因为它有几百行长,但这是我的菜单文件、头文件和游戏文件的开头:

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

这些是我正在努力处理的代码文件...请不要指出 system("pause") 效率不高,我已经知道了。我只需要知道如何在游戏开始 运行 时让我的用户输入正常工作。如果我的描述很糟糕,这些是我输出的照片。

菜单:

游戏

请帮忙!我明天需要这个才能工作,但我被卡住了!

编辑:感谢@ThomasMatthews 修复!!! 我通过放置 getline(cin,userInput) 修复了它;一个在另一个上面两次。不知道为什么它起作用了,但它确实起作用了!我的程序 运行 现在没问题了!谢谢!!!!

因为你执行 cin >> x 你的 STDIN 实际上在缓冲区中有一个额外的字符(当你按 enter 时换行符)

因此,您的 getline(...) 实际上是在获取缓冲区中已有的回车键。

您可以通过执行 cin.ignore(); 来解决这个问题;事先,或使用 getline(...) 而不是 cin >> x