所以我正在尝试制作一个简单的应用程序来测试用户.它会询问一个问题并且用户回答1,2,3或4.然后应用程序接受该答案,如果正确,则将total_score添加+1,最后将显示该值.一切看起来都很合理,但是当我运行它并且我到达该if (q1_valid == false)部分时它会跳过cout并运行goto,无论q1_valid是true还是false.
#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();
}
Run Code Online (Sandbox Code Playgroud)
我在这里有一些提示: