我在测验中遇到这个问题,如果我输入一个不是整数的答案,它会跳过所有问题并将其标记为正确。我怎样才能使非整数答案计为错误? 我是否需要使用' switch'而不是' if / else'声明?我试过了,即使是正确答案,它也会进入默认情况。
#include <iostream>
using namespace std;
int main()
{
float q1 = 1;
float q2 = 1;
float q3 = 2;
float q4 = 1;
float q5 = 2;
int total;
int Question_Score = 1;
cout << "Press 'Enter' to begin the quiz. \n";
cin.ignore();
cout << string(50, '\n');
cout << "Question 1: \n" << endl;
cout << "Which country started World War II? \n" << endl;
cout << "Please type the number of the answer you wish to use. \n";
cout << "1. Germany \n";
cout << "2. Soviet Union \n" << endl;
cin >> q1;
cout << endl;
if (q1 == 1) {
cout << "You are correct! \n" << "Now moving on to the next question. \n" << endl;
total += Question_Score;
}
else {
cout << "You are incorrect, better luck next time. \n" << "Now moving on to the next question. \n" << endl;
}
cout << "Question 2: \n" << endl;
cout << "Who was the leader of Germany in World War II? \n" << endl;
cout << "Please type the number of the answer you wish to use. \n";
cout << "1. Adolf Hitler \n";
cout << "2. Friedrich Wilhelm II \n" << endl;
cin >> q2;
if (q2 == 1) {
cout << "You are correct! \n" << "Now moving on to the next question. \n" << endl;
total += Question_Score;
}
else {
cout << "You are incorrect, better luck next time. \n" << "Now moving on to the next question. \n" << endl;
}
cout << "Question 3: \n" << endl;
cout << "Who won World War II? \n" << endl;
cout << "Please type the number of the answer you wish to use. \n";
cout << "1. Axis \n";
cout << "2. Allies \n" << endl;
cin >> q3;
if (q3 == 2) {
cout << "You are correct! \n" << "Now moving on to the next question. \n" << endl;
total += Question_Score;
}
else {
cout << "You are incorrect, better luck next time. \n" << "Now moving on to the next question. \n" << endl;
}
cout << "Question 4: \n" << endl;
cout << "Which countries made up the Axis powers? \n" << endl;
cout << "Please type the number of the answer you wish to use. \n";
cout << "1. Germany, Japan, and Italy \n";
cout << "2. Great Britain, United States, China, and the Soviet Union \n" << endl;
cin >> q4;
if (q4 == 1) {
cout << "You are correct! \n" << "Now moving on to the next question. \n" << endl;
total += Question_Score;
}
else {
cout << "You are incorrect, better luck next time. \n" << "Now moving on to the next question. \n" << endl;
}
cout << "Question 5: \n" << endl;
cout << "Which countries made up the Allied powers? \n" << endl;
cout << "Please type the number of the answer you wish to use. \n";
cout << "1. Germany, Japan, and Italy \n";
cout << "2. Great Britain, United States, China, and the Soviet Union \n" << endl;
cin >> q5;
if (q5 == 2) {
cout << "You are correct! \n" << "Now moving on to the next question. \n" << endl;
total += Question_Score;
}
else {
cout << "You are incorrect, better luck next time. \n" << "Now moving on to the next question. \n" << endl;
}
cout << "Congratulations! You have completed the quiz. \n" << endl;
cout << "Your score was: " << total;
cout << "/5 \n" << endl;
return 0;
}