Trying to make an error message

Hello,

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)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 # include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {

 int firstNum, secondNum, thirdNum;
    
    cout<< "Input integer 1: ";
    cin>> firstNum;
    cout<< "Input integer 2: ";
    cin>> secondNum;
    cout<< "Input integer 3: ";
    cin>> thirdNum;
    
    cout<< "Please select an order to sort by:\n"<<endl;
    cout<< "(A)scending order"<<endl;
    cout<< "(D)escending order\n"<<endl;
    
    char x;
    cin>> x;
    cout<<endl;
    
    if (x=='A'||x=='a'){
       
    if(firstNum<secondNum && firstNum<thirdNum && secondNum>thirdNum){
            cout<<endl;
            cout<< "Sorted Ascending: ";
            cout<<firstNum<<" <= "<< thirdNum<<" <= " <<secondNum<<endl;}
    
   
   if(x=='D'||x=='d'){
            
            if (firstNum>secondNum && firstNum>thirdNum && secondNum<thirdNum){
                    cout<<endl;
                    cout<< "Sorted Descending: ";
                    cout<<firstNum<<" >= "<< thirdNum<<" >= " <<secondNum<<endl;}
          
Last edited on
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:

1
2
3
    if (...) {
        do_something();
    }
1
2
3
4
    if (...)
    {
        do_something();
    }

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    std::cout << "Please select an order to sort by:\n"
              << "(A)scending order\n(D)escending order\n? " ;

    char ch = '*' ;
    do
    {
        std::cin >> ch ;
        ch = std::toupper(ch) ;
    }
    while( ch != 'A' && ch != 'D' && std::cout << "input error: enter either A or D? " ) ;

    std::cout << "you selected to sort by "
              << ( ch == 'A' ? "(A)scending" : "(D)escending" ) << " order\n" ;
}
Wohooo got that program working like a charm! I will definitely improve my coding style. Thanks Guys
Topic archived. No new replies allowed.