Using Loops or IF?

Hello. I am taking a beginner class on C++ and we have an assignment to make a 5 question, multiple-choice quiz with answers a-d using switch statements. If a user enters a letter that is not a-d, I don't want the program to continue on. I would like the program to stop and repeat the question. I'm not sure whether I should be using a loop or some kind of if statement. A small sample is below:

char response;
int numright = 0;
cout << "\t\t\tWELCOME TO MY QUIZ!\n" << endl;
cout << "Today's topic is NINTENDO!\n\n" << endl;
cout << "What is the name of Mario's archnemesis?" << endl;
cout << "\ta. Princess Peach" << endl;
cout << "\tb. Dry Bones" << endl;
cout << "\tc. Toad" << endl;
cout << "\td. Bowser\n" << endl;
cin >> response;

switch (response)
{

case 'A':
case 'a':
cout << "Sorry. Incorrect.\n " << endl;
break;
case 'B':
case 'b':
cout << "sorry. Incorrect.\n " << endl;
break;
case 'C':
case 'c':
cout << "Sorry. Incorrect.\n " << endl;
break;
case 'D':
case 'd':
cout << "Correct!\n " << endl;
cout << "\a\a\a\a\a" << endl;
numright++;
break;
default:
cout << "You must enter a letter a-d\n" << endl;
break;

}
I might use a while loop to validate the entry before entering the switch statement - while the input is invalid, prompt them to re-enter.

Since you're going to have 5 questions, I would probably create a separate function to do the validation, instead of repeating the same code 5 times.
I agree with the wildblue as you need to reuse the switch statement if the user input anything other than a-d.
Registered users can post here. Sign in or register to post.