0

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;

}
  • Check the value of cin to see if it is an integer, if not show an error message and re-ask the user to input their answer again. – Ryan Wilson Feb 23 '18 at 16:56
  • @RyanWilson -- `std::cin` is an object whose type is derived from `std::istream`. It will never be an integer. – Pete Becker Feb 23 '18 at 16:59
  • 1
    I know, that's what I meant by the value of cin, I meant the user input value, maybe I should have been more clear. – Ryan Wilson Feb 23 '18 at 17:00
  • Aside: You may find reading this of use https://newton.ex.ac.uk/teaching/resources/jmr/4.html (A resource explaining why making functions is of use) Granted it's for C; but the concepts still apply – UKMonkey Feb 23 '18 at 17:03

0 Answers0