I'm supposed to make an error message whenever a wrong input is given by the user. He is supposed to type 'A' or 'D' and it doesn't matter if it is lower case. Any other value should give an error. I tried " if (!(x='A')) cout<< "error message", but that just gave me an error with any value. So far this is what I've got. ( I'll only show a bit since it's quite long)
You have the right idea. The trick is to test whether the input is any incorrect answer.
One way to do that is to use if..else if...else chains.
Another is to use a switch statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
char choice;
cin >> choice;
switch (choice)
{
case'A': case'a':
// sort ascending
break;
case'D': case'd':
// sort descending
break;
default:
std::cout << "You did not enter A or D.\n";
return 1;
}
Hope this helps.
[edit]
Oh, just to add, your brace style is unfortunately popular with some, but it is not your friend. Whenever you type something that will need braces, the first thing you should do is put a closing brace on a line all by itself and line it up with the same indentation as the previous line:
1 2
if (...) {
}
1 2 3
if (...)
{
}
Both of these make life easier, because you then know to move the carat between the two braces and enter a new line and indent correctly: