Why must I put ; before cout when using multi-way if-else statement

Sep 1, 2014 at 2:33pm
I am using Microsoft Visual Studio 2010.

I am creating a shipping cost program that compares the package weight to 2 values followed by the output of the shipping cost. If the package weight is larger than the compared 2 values, the program compares the package weight again to another 2 values. This comparison happens up to a total of 4 times...when I get to the last comparison I start by typing:

else (comparison of package weight to 2 values)
cout << "The Shipping Cost is whatever";

In VS 2010, it underlines the cout in red, and it reads that ; must come before it. In my programming book it does not mention this. Why must I put ; before cout in order for it to compile the code? This is the first time I've had to place anything right before cout.
Sep 1, 2014 at 2:41pm
Post the entire if block.
Sep 1, 2014 at 3:20pm
Do you mean that you have:
1
2
3
4
if ( condition1 )
  statement1;
else ( condition2 )
  statement2;

If so, do note that there can be no condition after else.
Sep 1, 2014 at 3:41pm
1
2
3
4
5
6
7
8
if (condition1)
  statement1;
else if (condition2)
  statement2;
else if (condition3)
  statement3;
else (condition4)
  statement4;


Statement4 starts with cout << "text";

But after typing statement4, VS underlines cout and error: expected ";" before cout

I could paste the actual code but it is homework for a class so...

The if and else if are located with int main(), not sure if that makes a difference. I am pretty new to c++
Sep 1, 2014 at 3:50pm
@plp384

If there are only four conditions that are possible, you don't need the (condition4) after the last else. Or, you add the if after that last else, and add a
1
2
else
cout << "Sorry, do not understand that input." << endl;
to be printed if all the conditions, fail.
Sep 1, 2014 at 3:50pm
The (condition4) is not legal C/C++. The whole point of an else block is that it runs in all circumstances where none of the other conditions were met.
Sep 1, 2014 at 3:50pm
Yeah, like keskiverto said. There can be no condition after else.
Sep 1, 2014 at 4:11pm
@keskiverto I just looked at my textbook and it does not have a condition following else either in the c++ examples....

Thank You for a quick response.

@whitenite1 Thank You for posting that code, it reiterates that you cannot have a condition after else.

@MikeyBoy and @helios Thank You
Topic archived. No new replies allowed.