I am having this problem on my quiz where if I enter an answer that is not an integer it skips through all the questions and marks those as correct. How can I make it so non-integer answers count as incorrect? Do I need to use 'switch' instead of an 'if/else' statement? I tried that and it went to the default case even if it was the correct answer.
#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;
}