所以我试图做一个简单的应用程序,测验用户。它会提出一个问题,用户回答1,2,3或4.然后,应用程序将采用该答案,如果正确,则会在最后显示的total_score中添加+1。一切看起来都对我有用,但是当我运行它并且到达if (q1_valid == false)
部分时,它跳过cout并运行goto,无论q1_valid是true还是false。C++ if ... then语句
#include <iostream>
using namespace std;
int main()
{
int q1_answer;
int total_score;
bool q1_correct;
bool q1_valid;
Question_1:
cout << "Question 1 (#3 is correct)" << endl;
cout << "1.) Answer 1" <<endl;
cout << "2.) Answer 2" <<endl;
cout << "3.) Answer 3" <<endl;
cout << "4.) Answer 4" <<endl;
cin >> q1_answer;
if (q1_answer == 1)
q1_correct = false;
q1_valid = true;
if (q1_answer == 2)
q1_correct = false;
q1_valid = true;
if (q1_answer == 3)
q1_correct = true;
q1_valid = true;
if (q1_answer == 4)
q1_correct = false;
q1_valid = true;
if (q1_valid == false)
cout << "Invalid answer." <<endl;
goto Question_1;
if (q1_correct == true)
cout << "Correct!" <<endl;
(total_score + 1);
goto Question_2;
if (q1_correct == false)
cout << "Incorrect." <<endl;
goto Question_2;
if (q1_valid == false)
goto Question_1;
Question_2:
cout<< "Q2" <<endl;
cin.ignore();
cin.ignore();
}
用于下一次提示:反引号是主代码的报价。对于块引用代码,可以缩进四个空格或按下“{}”按钮。 – 2011-05-27 21:34:03
你使用C++多久了?你需要用花括号包装你的if语句。 – 2011-05-27 21:34:11
这是实际的代码吗?导致你的if似乎有很多陈述,但只有一个实际上与if相关,因为你不使用{} – 2011-05-27 21:35:16