Thread: I have a compiling error...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    14

    I have a compiling error...

    Well, I started with switches, and completely understand them and how they work. But, I have 2 errors when trying to compile my improved number game. Here is the code:

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
            //loop count
            int loopCount;
            cout<<"Enter number of guesses you wish to make:\n";
            cin>> loopCount;
    
            //entering guesses
            while(loopCount>0)
            {
                    loopCount=loopCount--;
                    int guess;
                    cout<<"Enter a guess:\n";
                    cin>> guess;
    
                    if(guess==88)
                    {
                    cout<<"That is correct! Press any key to exit.\n";
                    getch();
                    break;
                    }
    
                    else
                    switch(guess)
                    {
                            if(guess>88)
                            {
                                    //lower
                                    cout<<"That is incorrect. The number is lower. Only "<<loopCount<<" guesses remaining.\n";
                                    break;
      else
                                    //higher
                                    cout<<"That is incorrect. The number is higher. Only "<<loopCount<<" guesses remaining.\n";
                                    break;
                            }
                            break;
                    }
            }
            return 0;
    }
    The errors I get are:

    number.cpp(37)Error: parse error before 'else'
    number.cpp(32)Error: Warning: unreachable code at beginning of switch statement

    I have no idea how to fix either of those. I've been toying around with them for quite a while, but I'm clueless.

  2. #2
    Unregistered
    Guest
    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>

    int main()
    {
    int loopCount;
    cout<<"Enter number of guesses you wish to make:\n";
    cin>> loopCount;

    while(loopCount>0)
    {
    loopCount=loopCount--;
    int guess;
    cout<<"Enter a guess:\n";
    cin>> guess;

    if(guess==88)
    {
    cout<<"That is correct! Press any key to exit.\n";
    getch();
    break;
    }

    else
    {
    if(guess>88)
    cout<<"That is incorrect. The number is lower. Only "<<loopCount<<" guesses remaining.\n";
    else
    cout<<"That is incorrect. The number is higher. Only "<<loopCount<<" guesses remaining.\n";
    }
    }
    return 0;
    }

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    It seems like you are forgetting braces.

    For your if statements you have braces, but not for else's.
    Try including them

  4. #4
    Unregistered
    Guest
    switch is only used with case statements ex.

    switch(menuchoice)
    {
    case 1:
    //do something
    break;
    case 2:
    //do something else
    break;
    case default:
    //usually an error message or something
    }

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    >Switch is only used in case statments.

    I am extremely dumb......-_-

    Thanks for the help. I forgot about the braces around the else statements.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    You're not dumb, just learning. Hope I helped.
    Oh me == unregistered

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    Yeah, you helped. In C++ for dummies, it doesn't say you need braces before and after the 'else'. It doesn't even show that in example programs.

  8. #8
    Unregistered
    Guest

    Cool

    Now you realize the extent that dummy books have.

    Mr. C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM