I am really having trouble with the last for loop. I am supposed to display the question numbers that were answered incorrectly, not the actual answers. Thanks
I am having one last problem with my program. I cannot get the input validation to work correctly. It is making me input a char twice if it is enter correctly. Somewhere in there something if off. I have updated my code up top.
You are checking the answer and again asking the user to answer it... You have to take the input and then check for its validity...
this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for (int i = 0; i < 20; i++)
{
cout << "Enter your answer: ";
cin >> usersAnswers[i];
if(usersAnswers[i] == 'A')
cin >> usersAnswers[i];
elseif(usersAnswers[i] == 'B')
cin >> usersAnswers[i];
elseif(usersAnswers[i] =='C')
cin >> usersAnswers[i];
elseif(usersAnswers[i] == 'D')
cin >> usersAnswers[i];
else
cout << "You must enter A, B, C, D "<< endl;
}
should be
1 2 3 4 5 6 7 8 9 10 11 12 13
reattempt:
cout << "Enter your answer: ";
cin >> usersAnswers[i];
switch (usersAnswers[i])
{
case'A': case'B': case'C': case'D':
break;
default:
cout<<"cout << "You must enter A, B, C, D only"<< endl;";
goto reattempt;
break;//I dont think this break would be necessary would it be?
}
here I mention two things
1.switch-case was deliberately preferred over if-else as it is shorter... You could also have used ||(OR) operator but that would also be very long...
2.I have used a goto it is a bad habit.... You should sincerely try to avoid it on big codes... Alternatively you could have used a while loop
I tried to do a while loop. I don't understand what the heck is going on.
1 2 3 4 5 6 7 8
for (int i = 0; i < 20; i++)
{
cout << "Enter your answer: ";
cin >> usersAnswers[i];
while(usersAnswers[i] != 'A', 'B', 'C', 'D')
cout <<"You must enter A, B, C, D "<< endl;
cin >> usersAnswers[i];
}