C++ 链接的cpp文件-can';不要在其他文件中使用getline
我知道这似乎是一个简单的问题,但我有一个课堂作业,要做一个20个问题类型的游戏。我想我应该多做一点,做一份菜单。该程序运行良好,但当我输入实际的游戏文件时,它会输出第一行,然后转到我的最后一行,并输出。。。它不会让我打字和玩游戏。这容易修复吗 当我启动程序时,菜单会正常运行。它提示用户输入导航(1=玩游戏,2=说明,3=单词数据库,4=退出)。当我按1键支付游戏时,它会像应该的那样清除屏幕,但不会让我键入第一个问题(动物、食物、元素、其他)的答案,而是说“是动物、食物、元素或其他吗?(输入a、f、e或o进行选择)”,就像它应该的那样,但会将我输入的1作为该问题的输入,在提示输入关闭程序之前,使其输出“这不是选项!”。我怎样才能解决这个问题?我不喜欢包含我的所有代码,因为它只有几百行,但这是我的菜单文件、头文件和游戏文件的开头: myheader.h:C++ 链接的cpp文件-can';不要在其他文件中使用getline,c++,linker-errors,C++,Linker Errors,我知道这似乎是一个简单的问题,但我有一个课堂作业,要做一个20个问题类型的游戏。我想我应该多做一点,做一份菜单。该程序运行良好,但当我输入实际的游戏文件时,它会输出第一行,然后转到我的最后一行,并输出。。。它不会让我打字和玩游戏。这容易修复吗 当我启动程序时,菜单会正常运行。它提示用户输入导航(1=玩游戏,2=说明,3=单词数据库,4=退出)。当我按1键支付游戏时,它会像应该的那样清除屏幕,但不会让我键入第一个问题(动物、食物、元素、其他)的答案,而是说“是动物、食物、元素或其他吗?(输入a、f
#include <windows.h>
#include <iostream>
void runGame();
void showHowTo();
void showWords();
void mainMenu();
#包括
#包括
void runGame();
void showhoto();
void showWords();
void main菜单();
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;
}
#包括
#包括
#包括“myheader.h”
#包括
使用名称空间std;
int main()
{
int x=0;
cout因为您不使用cin>>x,所以您的STDIN实际上在缓冲区中有一个额外的字符(按enter键时的新行)
因此,您的getline(…)实际上正在获取缓冲区中已经存在的enter键
您可以通过预先执行cin.ignore();或使用getline(…)来修复此问题您的输出图像对我来说是断开的链接,而不是cin>>x。@JGroven右键单击并在新选项卡中打开。不确定它们为什么不起作用:/如果您无法获得它们,它们就是:@JGroven在比较之前使用std::tolower
或std::toupper
将您的比较减少一半。添加cout-Fixed!谢谢!答案是比其他人更有礼貌…谢谢!
#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");
}