【发布时间】:2016-09-08 21:42:49
【问题描述】:
我目前正在尝试用 C++ 创建一个基本的问答游戏。
以下代码在我尝试运行时会引发错误,如果我不在主类中使用 answer("answer") 而是将其替换为实际代码,则它可以工作。
我想“嵌套”(我不知道技术术语)一些代码,这样我就不必每次都写出来,正如你所看到的,我希望写下任何问题答案(“答案”)。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "QUIZ C++ EDITION!\n";
cout << "Question 1:\n";
cout << "What is the colour you get when you mix red and yellow?\n\n";
question("Orange");
system("PAUSE");
}
void question(string answer) {
string input;
getline(cin, input);
if (input == answer) {
cout << "\nCorrectimundo!\n";
}
else
{
cout << "\nWrongimundo.\n";
}
return;
}
我感觉这是语法错误的情况,但不幸的是 IDE 没有显示错误的位置,它仅在我运行程序时发生。
【问题讨论】:
-
运行时遇到什么错误?
标签: c++ input namespaces std iostream