if/else trouble

Discussion in 'Programmer's Corner' started by sgardner025, Sep 2, 2010.

  1. sgardner025

    Thread Starter Active Member

    Nov 5, 2009
    79
    4
    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

    Code ( (Unknown Language)):
    1. 1 #include <iostream>
    2. 2
    3. 3 using namespace std;
    4. 4 typedef unsigned short int ushort;
    5. 5
    6. 6 int main()
    7. 7 {
    8. 8   ushort a;
    9. 9   ushort b;
    10. 10  cout << "Enter a whole number less than ten: ";
    11. 11  cin >> a;
    12. 12
    13. 13  if (a < 10)
    14. 14  {
    15. 15      if (a > 0)
    16. 16          cout << "Enter a whole number less than ten: ";
    17. 17          cin >> b;
    18. 18          ushort c = a + b;
    19. 19      {
    20. 20          if (b < 10)
    21. 21          {
    22. 22              if (b > 0)
    23. 23                  cout << "the sum = " << c << "\n";
    24. 24              {
    25. 25                  if (c <=18)
    26. 26                  {
    27. 27                      if (c >=2)
    28. 28                          cout << "Correct!";
    29. 29                      else
    30. 30                          cout << "Incorrect!";
    31. 31                  }
    32. 32                  else
    33. 33                      cout << "Incorrect!";
    34. 34              }
    35. 35              else
    36. 36                  cout << "Error!";
    37. 37          }
    38. 38          else
    39. 39              cout << "Error!";
    40. 40     }
    41. 41      else
    42. 42          cout << "Error!";
    43. 43  }
    44. 44  else
    45. 45      cout << "Error!";
    46. 46
    47. 47  cin.get();
    48. 48  return 0;
    49. 49}
    50.  
    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: Sep 2, 2010
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    The correct way to write is:

    if ()
    {
    .......
    }

    else if ()
    {
    .........
    }

    else
    {
    ........
    }
     
  3. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    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
     
  4. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    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.
     
  5. sgardner025

    Thread Starter Active Member

    Nov 5, 2009
    79
    4
    I intended for the else at 35 to follow the if at 22. Is that not the case here?
     
  6. DonQ

    Active Member

    May 6, 2009
    320
    11
    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.
     
  7. sgardner025

    Thread Starter Active Member

    Nov 5, 2009
    79
    4
    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.
     
  8. sgardner025

    Thread Starter Active Member

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

    Code ( (Unknown Language)):
    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.     {
    17.             cout << "Enter a whole number less than ten: ";
    18.             cin >> b;
    19.     }
    20.     else
    21.     {
    22.         cout << "Error!";
    23.     }
    24.     }
    25.     else
    26.         cout << "Error!";
    27.  
    28.     if (b < 10)
    29.     {
    30.         if (b > 0)
    31.         {
    32.             ushort c = a + b;
    33.             cout << "the sum = " << c << "\n";
    34.         }
    35.         else
    36.         {
    37.             cout << "Error!";
    38.         }
    39.     }
    40.         else
    41.             cout << "Error!";
    42.  
    43.     ushort c = a + b;
    44.     if (c <=18)
    45.     {
    46.         if (c >=2)
    47.     {
    48.           cout << "Correct!";
    49.     }
    50.     else
    51.     {
    52.         cout << "Incorrect!";
    53.     }
    54.     }
    55.     else
    56.       cout << "Incorrect!";
    57.  
    58.     cin.get();
    59.     return 0;
    60. }
    61.  
     
  9. DonQ

    Active Member

    May 6, 2009
    320
    11
    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:
    Code ( (Unknown Language)):
    1.  
    2.     if (a < 10)
    3.     {
    4.         if (a > 0)
    5.         {
    6.              cout << "Enter a whole number less than ten: ";
    7.              cin >> b;
    8.         }
    9.         else
    10.         {
    11.              cout << "Error!";
    12.         }
    13.     }
    14.     else
    15.         cout << "Error!";
    16. ...
    17.  
    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:
    Code ( (Unknown Language)):
    1.    
    2.     if (a < 10) {
    3.         if (a > 0) {
    4.             cout << "Enter a whole number less than ten: ";
    5.             cin >> b;
    6.        } else {
    7.             cout << "Error!";
    8.        }
    9.     } else cout << "Error!";
    10. ...
    11.  
    Or even:
    Code ( (Unknown Language)):
    1.    
    2.     if ((a < 10) && (a > 0)) {
    3.          cout << "Enter a whole number less than ten: ";
    4.          cin >> b;
    5.     } else cout << "Error!";
    6. ...
    7.  
    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.
     
  10. sgardner025

    Thread Starter Active Member

    Nov 5, 2009
    79
    4
    Thanks for the examples and tips. I'll work on formatting and legibility for sure.
     
  11. sgardner025

    Thread Starter Active Member

    Nov 5, 2009
    79
    4
    Is this any better?

    Code ( (Unknown Language)):
    1. #include <iostream>
    2.  
    3. using namespace std;
    4. typedef unsigned short int ushort;
    5.  
    6. int main()
    7. {   ushort a, b, c;
    8.     cout << "Enter a whole number less than ten: ";
    9.     cin >> a;
    10.     if (a < 10 && a > 0)
    11.         {cout << "Enter a whole number less than ten: ";
    12.          cin >> b;}
    13.     else{cout << "Error!\n";
    14.          goto end;}
    15.  
    16.     if (b < 10 && b > 0)
    17.         {c = a + b;
    18.          cout << "the sum = " << c << "\n";}
    19.     else{cout << "Error!\n";}
    20.  
    21.     end:
    22.     c = a + b;
    23.     if ((c <= 18 && c >= 2) && (a,b < 10 && a,b > 0))
    24.         {cout << "Correct!";}
    25.     else{cout << "Incorrect!";}
    26.  
    27.     cin.get();
    28.     return 0;}
    29.  
    30.  
    31.  
    32.  
     
    Last edited: Sep 3, 2010
  12. DonQ

    Active Member

    May 6, 2009
    320
    11
    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.

    Code ( (Unknown Language)):
    1.  
    2. { // this is the start of one block
    3.     all the code within it goes with the same indent
    4.     more code
    5.     {  // if you have another block, the brace starts at the outer blocks indent
    6.          all the code for this sub-block gets indented again
    7.          more code
    8.     } // end of the inner block
    9.     more code of the outer block
    10.     more code
    11. } // end of outer block
    12.  
    Your code:
    Code ( (Unknown Language)):
    1.  
    2.     if (a < 10 && a > 0)
    3.         {cout << "Enter a whole number less than ten: ";
    4.          cin >> b;}
    5.     else{cout << "Error!\n";
    6.          goto end;}
    7.  
    Would probably be better as:
    Code ( (Unknown Language)):
    1.  
    2.     if (a < 10 && a > 0)
    3.     {    
    4.         cout << "Enter a whole number less than ten: ";
    5.         cin >> b;
    6.     }
    7.     else
    8.     {
    9.         cout << "Error!\n";
    10.         goto end;
    11.     }
    12.  
    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:
    Code ( (Unknown Language)):
    1.  
    2.     if (a < 10 && a > 0) {    
    3.         cout << "Enter a whole number less than ten: ";
    4.         cin >> b;
    5.     } else {
    6.         cout << "Error!\n";
    7.         goto end;
    8.     }
    9.  
    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.
     
  13. sgardner025

    Thread Starter Active Member

    Nov 5, 2009
    79
    4
    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.
     
  14. DonQ

    Active Member

    May 6, 2009
    320
    11
    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.
     
Loading...