我知道这可能看起来像一个简单的问题,但我有一个课堂作业,使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");
}
这些都是代码文件,我挣扎......请不要指出系统(“暂停”)效率不高,我已经知道了。我只需要知道如何在游戏开始运行时让我的用户输入工作。如果我的描述很糟糕,这些是我输出的照片。
菜单:
游戏
请帮帮忙!我需要这个为明天工作,我卡住了!
编辑:@ThomasMatthews固定感谢! 我通过把getline(cin,userInput);两次一个在另一个之上。不知道它为什么起作用,但它确实!我的程序现在运行良好!谢谢!!!!
您的输出图像是我的破碎链接。 – JGroven
@JGroven右键单击并在新选项卡中打开。不知道为什么他们不工作:/他们是,如果你不明白的话: –
@JGroven [菜单](https://ibb.co/g9uttv)[游戏](https://ibb.co/ huQNmF) –