how to return to beginning of the case if user press wrong Character?

Write your question here.

#include <string>
#include <cmath>
using namespace std;



int main(){
int score = 0;
int count = 0;
char answer;
int r=1;


cout<<"please use small letter to answer"<<endl;


switch(r)
{
case 1:
cout << "answer #1? " << endl;
cin >> answer;
if (answer == 'b')
{
cout << "Correct" << endl;
score++;
}
else if(answer=='a'||answer=='c'||answer=='d')
{
cout << "Incorrect" << endl;
cout << "The correct answer is B" << endl;
}
else
{
cout<<"enter valid char"<<endl;

}
I'm trying to make multiple choice programming
I'm confusing about input validation
I want answer only accept a, b ,c or d,
and if I put wrong words or number I want to repeat the case 1 until user put a, b ,c or d.
What should I add on this code?

Last edited on
1
2
3
4
while ( cin >> answer && ! ('a'==answer||...) )
  {
    cout << "You must answer one of a, b, c, or d.  Try again:" << endl;
  }

Or something like that. The main thing is the use of loop that repeats until something.
Topic archived. No new replies allowed.