How to display error if user enters anything other than A, B, C, D.

I'm having trouble displaying an error message if the user does not enter A, B, C, D.


#include <iostream>
#include <conio.h>
using namespace std;

int main ()
{
const int answers = 20;
char correctAnswers[answers] = {'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A'};
char studentAnswers[answers];
int count = 0;
int correct = 20;
int incorrect;

//Get the student's answers.
cout << "Please enter the student's answers using capital letters: ";
cin >> studentAnswers[0];
cin >> studentAnswers[1];
cin >> studentAnswers[2];
cin >> studentAnswers[3];
cin >> studentAnswers[4];
cin >> studentAnswers[5];
cin >> studentAnswers[6];
cin >> studentAnswers[7];
cin >> studentAnswers[8];
cin >> studentAnswers[9];
cin >> studentAnswers[10];
cin >> studentAnswers[11];
cin >> studentAnswers[12];
cin >> studentAnswers[13];
cin >> studentAnswers[14];
cin >> studentAnswers[15];
cin >> studentAnswers[16];
cin >> studentAnswers[17];
cin >> studentAnswers[18];
cin >> studentAnswers[19];


if ( studentAnswers[answers] != ('A' || 'B' || 'C' || 'D'))
{
cout << "Invalid entry.\n";
cout << "Entrys must be A, B, C, or D.\n";
cout << "Please restart program entering valid
answers.\n";
}[/b]

for (count=0 ; count < answers ; count++)
{
if (correctAnswers[count] != studentAnswers[count])

cout << "Answer " << count + 1 << " was incorrect.\n";

}



for (count=0 ; count < answers ; count++)
{
if (correctAnswers[count] != studentAnswers[count])

correct--;


}

cout << "The number of correct answers the student answered was " << correct << endl;

incorrect = 20 - correct;

cout << "The number of incorrect answers the student answered was " << incorrect << endl;

_getch ();
return 0;
}
Last edited on
In addition to enumerating how many correct answers there were, you can print out the question numbers of incorrect problems. Getting the number incorrect is just a matter of subtracting the correct from the total.

Also, please use [code][/code] tags in the future.
OK. But i do not know what tags are
Just copy the code tags (the things in [square brackets] that Zhuge supplied) tags and place your code between them. Or click the buttono on the side of the reply box for code tags (it's a button that looks like this <> ) and place the code between the opening and closing tags.

Zhuge, how did you write that? Is it a secret or am I retarded?
Last edited on
Stick another set of tags inside them and it won't be parsed. Like so:
[co[tt][/tt]de][/code]
becomes
[code][/code]
[code]
#include <iostream>

int main ()
{
std::cout<< "Brilliant! Thank you very much.";
return 0;
}
[/code]

1
2
3
4
5
6
7
#include <iostream>

int main ()
{
     std::cout<< "Brilliant! Thank you very much.";
     return 0;
}


(You have to add the spacing/ indenting manually, it won't do it for you. This is important because members constantly ask to keep it formatted as it is easier to read with proper indentation)

Hey hey, it works! That's great, thanks.
Topic archived. No new replies allowed.