我目前正在尝试用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;
}
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,这是一个错误的语法的情况,但IDE没有告诉我错误在哪里,它只发生在我运行程序时.