Thread: Help me out with this...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Help me out with this...

    Ok am so green to C++ it's scary! So please bear with me. I keep getting this error when I compile the code and it reads

    parse error before `else'
    It's real simple and to me it looks like it should work.

    Code:
    #include <iostream.h>
    
    
    int main()
    
    {
    
    int varNumber; //variable to place number in??
    
    cout<<"Please enter any number:  ";  //get input from user??
    
    cin>>varNumber; //place input from user into the varible??
    
    
    
    if(varNumber==8);
    
        {
    
        cout << "Right" <<endl;      
    
        }
    
    else
    
        {
    
        cout << "wrong" endl; 
    
        }
    
    return 0;
    
    }

  2. #2
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    Try this :

    code:
    -------------------------------------------------------------------------

    #include <iostream.h>


    int main()
    {
    int varNumber; //variable to place number in??

    cout<<"Please enter any number: "; //get input from user??
    cin>>varNumber; //place input from user into the varible??

    if (varNumber==8)
    cout << "Right" <<endl;
    else
    cout << "wrong" << endl;

    return 0;

    }

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    Thanks Ninja! It worked. The tutorial from the site instructed me to have { and } brackets for the code I want executed for the 'if' and 'else' statements. Guess it was wrong.

    What if I wanted to add code after the else statement that has nothing do with that if/else statement? Wouldn't the code assume that the code I add is part of the else statement? How do I end it? Is is just an 'End if' or something like that?



    Originally posted by biosninja
    Try this :

    code:
    -------------------------------------------------------------------------

    #include <iostream.h>


    int main()
    {
    int varNumber; //variable to place number in??

    cout<<"Please enter any number: "; //get input from user??
    cin>>varNumber; //place input from user into the varible??

    if (varNumber==8)
    cout << "Right" <<endl;
    else
    cout << "wrong" << endl;

    return 0;

    }

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I almost always use curly braces {} for if elses, it makes it much clearer.

    If you leave them out only the line immediately following the if or else is considered part of the conditional bit. If you see what I mean....
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    Ok, here's the deal.

    The copiler takes onely one sttement after an if/else, anything after that, it just goes on normal with. as for the braces ({}), the tutorial was right about that, It's better code for when the program will be updated in the future so the updater don't have to put them in.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    I see what you mean. I changed it back to use the brackets.....it helps me see and read the code alot clearer.

    Thanks guys you have been a great help!

    Originally posted by Salem
    > Guess it was wrong.
    No, it was quite correct

    The problem started here
    if(varNumber==8);
    See that ; at the end?

    This is equivalent to
    Code:
    if(varNumber==8) {
      // do nothing
    } else {
      // do nothing
    }
    Since you can add a new pair of {} in your code pretty much whenever you like
    Code:
        {
            cout << "Right" <<endl;     
        }
    had nothing at all to do with the if statement, and you would have seen "Right" output all the time regardless.

    By the time the compiler got to the 'else', all trace of processing an 'if' was gone, and the 'else' became a totally unexpected thing to see in the code.
    Thus, the error message
    parse error before `else'

  7. #7
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    When dealing with small programs it's easy to keep your code close together (and very neat). I've sorted your code out a bit, take note. For single line code inside if statments (such as a breif cout screen print) it is easier (also neater) to include your code on the same line.

    Code:
    #include <iostream>     //Standard header file
    int main()              
    {
        cout << "Please enter any number: ";  
        int answer; cin >> answer;
    
        //No curly brackets :)
        if(answer == 8) cout << "Right" <<endl;  
        else cout << "Wrong" << endl;
        return 0;
    }

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >When dealing with small programs it's easy to keep your code close together (and very neat).
    Compact isn't always neat, and even small programs should try to be readable. I'll very rarely place the body of an if statement on the same line as the condition because people are conditioned to look at the condition and then move to the next line for the body. I like my code to be readable so that you can run through it and not have to stop here and there to figure out what's going on:
    Code:
    #include <iostream>
    
    int main()
    {
      int answer;
    
      std::cout<<"Please enter any number: ";
    
      std::cin>> answer;
      std::cin.ignore(); // Eat leftover characters
    
      if ( answer == 8 )
        std::cout<<"Right"<<std::endl;
      else
        std::cout<<"Wrong"<<std::endl;
    
      std::cin.get(); // Pause at the end
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed