; missing before cout

The following code gives me error "expected ';' before 'cout'.

1
2
3
4
5
6
  if (select == 'A' || select == 'a')
        cout << "You are in car care \n\n";
  else if(select == 'B' || select == 'b')
    cout << "You are in backup \n\n";
  else (select == 'C' || select == 'c')
    cout << "You are in archiving \n\n";


but the following runs fine without error

1
2
3
4
5
6
  if (select == 'A' || select == 'a')
        cout << "You are in car care \n\n";
  else if(select == 'B' || select == 'b')
    cout << "You are in backup \n\n";
  else if(select == 'C' || select == 'c')
    cout << "You are in archiving \n\n";


Thank you for your input
That is correct. else is followed by a statement or a compound statement. L5 of the first is an else not followed by a statement as (select...) cout is not a valid statement. The else if in L5 of the second is a valid statement ( if (...) is a statement) and so compiles OK. You might want the second example to be:

1
2
3
4
5
6
7
8
if (select == 'A' || select == 'a')
    cout << "You are in car care \n\n";
else if(select == 'B' || select == 'b')
    cout << "You are in backup \n\n";
else if(select == 'C' || select == 'c')
    cout << "You are in archiving \n\n";
else
    cout << "Invalid selection\n\n";

Last edited on
In line 5 of the first snippet, you have an else statement. It takes the next statement and considers it the body of the else.

Because you do not have an if following the else, the conditional statement at the end of line 5 is considered to be body of the else. Line 6 is not part of the if/else if/else structure at all.

Because line 5 does not have a semicolon, the compiler thinks there is an error because the end of line 5 runs into line 6, and there is not semicolon to separate them.

The second code snippet is what you probably want and is correct.
to say it another way...
(select == 'C' || select == 'c')
what do you think this does, with no if around it?
If/else blocks are extremely susceptible to human error where we want to type the logic like a human thinks, not how a computer needs it. You frequently see errors like if(x == a || b) where the coder meant (x==a || x==b) but instead got "if x is a or b is not zero" instead and then their logic goes derp and bugs up.
you want to tie the above to the else without the if, because a human would say it that way, but its not what the robot wants.

a slightly related topic, expressions are statements, and you can do all kinds of cool stuff with them. eg x = (a==b)*42; //if a and be are the same, its 42, if not, x will be zero!
Last edited on
1
2
3
4
5
6
  if (select == 'A' || select == 'a')
        cout << "You are in car care \n\n";
  else if(select == 'B' || select == 'b')
    cout << "You are in backup \n\n";
  else (select == 'C' || select == 'c')
    cout << "You are in archiving \n\n";

The body of if and else can have either one statement, or a block with statements.
One can trivially add the block around single statement:
1
2
3
4
5
6
7
8
9
if (select == 'A' || select == 'a') {
    cout << "You are in car care \n\n" ;
}
else if (select == 'B' || select == 'b') {
    cout << "You are in backup \n\n" ;
}
else {
    (select == 'C' || select == 'c') cout << "You are in archiving \n\n" ;
}

Does the line 8 now look as suspicious as it should?


Is there even such thing as "else if"?
1
2
if ( condition ) statement-true
if ( condition ) statement-true else statement-false

In the above example there is if-statement:

1
2
3
4
if (select == 'A' || select == 'a')
    statement-true
else
    statement-false

where
1
2
3
4
5
6
7
8
9
// statement-true  is:
    cout << "You are in car care \n\n";
// statement-false is:
if (select == 'B' || select == 'b') {
    cout << "You are in backup \n\n" ;
}
else {
    (select == 'C' || select == 'c') cout << "You are in archiving \n\n" ;
}

The statement-false is an if-statement. It in turn has:
1
2
3
4
5
6
// condition:
    select == 'B' || select == 'b'
// statement-true:
    cout << "You are in backup \n\n";
// statement-false:
    (select == 'C' || select == 'c') cout << "You are in archiving \n\n" ;

Last edited on
seeker62 wrote:
The following code gives me error "expected ';' before 'cout'.

This is a situation where the compiler's expectation is different from the expectation of humans.

Adding the semicolon before cout would make the code compile. It would mean (select == 'C' || select == 'c'); becomes a statement, but it's a pretty useless statement since it doesn't do anything (it has no side-effects), and the "cout statement" would no longer be part of the if statement (it would always run regardless of select's value), so it wouldn't do what you want.

As a human it would make more sense to hear that if is expected after else (or before (select == 'C' || select == 'c')) but compilers aren't always clever like that.
Last edited on
Thank you all. Very simpe overlook on my part. I am brushing up my c++ knowledge of 20 years ago. A lot of dust to get rid of.
If you don't already know of it, have a look at:
https://www.learncpp.com/

C++ has changed massively over the previous 20 years (that version was probably C++98)! There has been four versions since (11, 14, 17, 20 - they now come every 3 years). The current version is C++20 with C++23 release imminently.
Last edited on
Topic archived. No new replies allowed.