if/else trouble

Thread Starter

sgardner025

Joined Nov 5, 2009
79
I have been reading a book on C++ for a few days and just put this code together to play with if/else commands. There are a few errors, as follows.

line 35 error: expected '}' before 'else'
line 38 error: expected '}' before 'else'
line 41 error: expected unqualified-id before 'else'
line 43 error: expected declaration before '}' token

Rich (BB code):
1 #include <iostream>
2
3 using namespace std;
4 typedef unsigned short int ushort;
5
6 int main()
7 {
8   ushort a;
9   ushort b;
10  cout << "Enter a whole number less than ten: ";
11  cin >> a;
12
13  if (a < 10)
14  {
15      if (a > 0)
16          cout << "Enter a whole number less than ten: ";
17          cin >> b;
18          ushort c = a + b;
19      {
20          if (b < 10)
21          {
22              if (b > 0)
23                  cout << "the sum = " << c << "\n";
24              {
25                  if (c <=18)
26                  {
27                      if (c >=2)
28                          cout << "Correct!";
29                      else
30                          cout << "Incorrect!";
31                  }
32                  else
33                      cout << "Incorrect!";
34              }
35              else
36                  cout << "Error!";
37          }
38          else
39              cout << "Error!";
40     }
41      else
42          cout << "Error!";
43  }
44  else
45      cout << "Error!";
46
47  cin.get();
48  return 0;
49}
I apologize for not having comments in there. If needed I can post some. I'm using code blocks 10.05. I also wanted to ask about the if statement on line 15. Is all that legal?
 
Last edited:

debjit625

Joined Apr 17, 2010
790
This is how I write in application programming ...

if(condition)
{
}
else
{
}

Or if I need nested conditional statement ..

if(condition)
{
if(condition)
{
}
else
{
}
}
else
{
if(condition)
{
}
else
{
}
}

Always try to use braces "{ }" to avoid these king of problems

Good luck
 

DumboFixer

Joined Feb 10, 2009
217
Your problem lies at line 23. You have a condition check at line 22 with a line of code at line 23 which will be executed if the condition is true. At line 24 you then start a code block which has an "else" at line 35. This else has no associated "if" as you've already used that at line 23.

The advice given above is well worth following. I thing I try to do is whenever I start a code block etc with a "{" I put in the closing "}" straight away so it's not forgotten and its positioning is clear.
 

DonQ

Joined May 6, 2009
321
I think your problem may begin on line 15. Even though the next 3 lines are indented, there is no '{' after the 'if' on line 15. The '{' on line 19 is all by itself, and has nothing to do with the previous 'if'. I can't really follow what you're trying to do with the formatting seeming to contradict what the braces say.

Also, lots of people say to format with all the braces on lines by themselves. K&R (the inventers of C) say something quite different, so it's a bit extreme to say "the correct way is (fill in your preference here)".

(Also realize, when you try to show code formatting in the main editing window, all the spaces often get stripped out and the formatting you were trying to show can get mangled beyond recognition. Thus the existence of the "code" tags.)

What is important is to have a standard, and then follow it. Your code does not, and I think the basis of the problem is there.
 

Thread Starter

sgardner025

Joined Nov 5, 2009
79
I was under the impression that the '{' on 19 did go with the 'if' on 15 and I intended for the '}' on 40 to be the closing for that section. I'll write it again and try different formatting.
 

Thread Starter

sgardner025

Joined Nov 5, 2009
79
OK it's getting better but not perfect. Thanks guys. Here is what I came up with.

Rich (BB code):
#include <iostream>

using namespace std;
typedef unsigned short int ushort;

int main()
{
    ushort a;
    ushort b;
    cout << "Enter a whole number less than ten: ";
    cin >> a;

    if (a < 10)
    {
        if (a > 0)
    {
            cout << "Enter a whole number less than ten: ";
            cin >> b;
    }
    else
    {
        cout << "Error!";
    }
    }
    else
        cout << "Error!";

    if (b < 10)
    {
        if (b > 0)
        {
            ushort c = a + b;
            cout << "the sum = " << c << "\n";
        }
        else
        {
            cout << "Error!";
        }
    }
        else
            cout << "Error!";

    ushort c = a + b;
    if (c <=18)
    {
        if (c >=2)
    {
          cout << "Correct!";
    }
    else
    {
        cout << "Incorrect!";
    }
    }
    else
      cout << "Incorrect!";

    cin.get();
    return 0;
}
 

DonQ

Joined May 6, 2009
321
The whole block, marked by braces and statements at lines 16,19,20,21,23 is a sub unit, and needs to be logically indented. That way it's more readable, and you don't wind up with constructs like the two aligned brackets on lines 23/24.

More like:
Rich (BB code):
    if (a < 10)
    {
        if (a > 0)
        {
             cout << "Enter a whole number less than ten: ";
             cin >> b;
        }
        else
        {
             cout << "Error!";
        }
    }
    else
        cout << "Error!";
...
There are several other similar formatting errors further along in the code.

In another coding style, the type that I and K&R like, it would look like this:
Rich (BB code):
    if (a < 10) {
        if (a > 0) {
            cout << "Enter a whole number less than ten: ";
            cin >> b;
       } else {
            cout << "Error!";
       }
    } else cout << "Error!";
...
Or even:
Rich (BB code):
    if ((a < 10) && (a > 0)) {
         cout << "Enter a whole number less than ten: ";
         cin >> b;
    } else cout << "Error!";
...
I can tell what this does by just glancing at it. The problem with the more expanded style is that even simple functions sprawl across (and out the bottom of) a whole screen.

Even though the code as you wrote it may work... Writing good code is not writing something that you can get to work, it is writing something that someone else can get to work.
 

Thread Starter

sgardner025

Joined Nov 5, 2009
79
Is this any better?

Rich (BB code):
#include <iostream>

using namespace std;
typedef unsigned short int ushort;

int main()
{   ushort a, b, c;
    cout << "Enter a whole number less than ten: ";
    cin >> a;
    if (a < 10 && a > 0)
        {cout << "Enter a whole number less than ten: ";
         cin >> b;}
    else{cout << "Error!\n";
         goto end;}

    if (b < 10 && b > 0)
        {c = a + b;
         cout << "the sum = " << c << "\n";}
    else{cout << "Error!\n";}

    end:
    c = a + b;
    if ((c <= 18 && c >= 2) && (a,b < 10 && a,b > 0))
        {cout << "Correct!";}
    else{cout << "Incorrect!";}

    cin.get();
    return 0;}
 
Last edited:

DonQ

Joined May 6, 2009
321
Now you're sort of mixing styles.

The purpose of just about any style is to allow you to see at a glance where one block begins and and ends. This is done by having them start and end in the same column. The style many use, and seems closest to the style you are trying to use, goes like this.

Rich (BB code):
{ // this is the start of one block
    all the code within it goes with the same indent
    more code
    {  // if you have another block, the brace starts at the outer blocks indent
         all the code for this sub-block gets indented again
         more code
    } // end of the inner block
    more code of the outer block
    more code
} // end of outer block
Your code:
Rich (BB code):
    if (a < 10 && a > 0)
        {cout << "Enter a whole number less than ten: ";
         cin >> b;}
    else{cout << "Error!\n";
         goto end;}
Would probably be better as:
Rich (BB code):
    if (a < 10 && a > 0)
    {    
        cout << "Enter a whole number less than ten: ";
        cin >> b;
    }
    else
    {
        cout << "Error!\n";
        goto end;
    }
Here, the brackets that begin and end each logical level of code are in the same column. If you want to find the end of the block, you just go straight down the page in the same column.

I (and a significant subset) prefer this style:
Rich (BB code):
    if (a < 10 && a > 0) {    
        cout << "Enter a whole number less than ten: ";
        cin >> b;
    } else {
        cout << "Error!\n";
        goto end;
    }
Each level begins with a brace at the end of the line, and closes with a brace under the first column of the statement that started that block. 'else' uses both of these rules on the same line. This style doesn't give you as big a number when you're bragging about "my program has this many kajillion lines", but it sure lets you put more on a page.

Either way, the object is to not get into the situation where you can't tell which '}' belongs to which '{', and have your compiler start saying things like:

line 35 error: expected '}' before 'else'
line 38 error: expected '}' before 'else'
line 41 error: expected unqualified-id before 'else'
line 43 error: expected declaration before '}' token

when the first error was actually quite a bit before line 35. So, in this case, even the compiler couldn't figure out what was going on.

It would probably help to just find some well written code in the style that you want to use and study it to see how the formatting works.
 

Thread Starter

sgardner025

Joined Nov 5, 2009
79
Thanks again for the examples. I like your last one and will use it as a guide. I've been writing little bits to toy with and find a new way of doing something about every time I look at one. For now I will continue through my book. BTW the book is SAMS Teach Yourself C++ in 24 Hours, fourth edition. It has been a good read so far.
 

DonQ

Joined May 6, 2009
321
One last advice, and programmers are real sticklers about this one. Avoid 'goto'. It's hardly ever needed, and takes away from being able to see the program flow based on the formatting.

Occasionally it is needed, but not for basic program flow.
 
Top