Thread: Very Basic Problem ( error: 'else' without a previous 'if' ) yet i have 'if' there :S

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    Very Basic Problem ( error: 'else' without a previous 'if' ) yet i have 'if' there :S

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        int number1;
    
        cout<< "Please Enter A Number: ";
        cin>> number1;
        cin.ignore();
        if ( number1 > 10 ); {
            cout<< "I Told You It Had To Be Less Than 10!\n";
            cout<< "No More Chances For You!\n";
            cout<< "Press Enter To Close";
        }
        else {
            cout<< "Well Done! You Entered: " << number1 << "Which Is Less Than 10, Just As I Asked!\n";
            cout<< "Now Press Enter Please!";
        }
        cin.get();
    }
    This is the code i have entered, as you can see it is a very basic code.

    Could someone please tell me where i have gone wrong? as i get the error code mentioned in the title.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Quote Originally Posted by AndiWhitts View Post
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        int number1;
    
        cout<< "Please Enter A Number: ";
        cin>> number1;
        cin.ignore();
        if ( number1 > 10 ); {
            cout<< "I Told You It Had To Be Less Than 10!\n";
            cout<< "No More Chances For You!\n";
            cout<< "Press Enter To Close";
        }
        else {
            cout<< "Well Done! You Entered: " << number1 << "Which Is Less Than 10, Just As I Asked!\n";
            cout<< "Now Press Enter Please!";
        }
        cin.get();
    }
    This is the code i have entered, as you can see it is a very basic code.

    Could someone please tell me where i have gone wrong? as i get the error code mentioned in the title.

    Thanks in advance!
    I rewrote the whole code and it works now but can someone please tell me where i went wrong for future reference?

    This is the new code that works:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        int number1;
    
        cout<< "Please Enter A Number that is less than 10: ";
        cin>> number1;
        cin.ignore();
        if (number1 > 10) {
        cout<<"Oh dear, you entered: " << number1 << ". I said it had to be less than 10!\n";
        }
        else {
        cout<<"Good job! You entered: " << number1 << ". Which is less than 10\n";
        }
        cin.get();
    }

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't put a semicolon after the if condition.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    ahh yes! thanks very much!

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    44
    i think you yourself removed the semicolon, so reason for correct behavior is evident.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  2. Replies: 2
    Last Post: 11-01-2010, 10:26 PM
  3. Replies: 1
    Last Post: 04-21-2008, 02:27 AM
  4. BAsic problem with error handling in sdl using printf
    By redwing26 in forum Game Programming
    Replies: 2
    Last Post: 08-01-2006, 05:45 AM
  5. Basic compiler error
    By sammacs in forum C++ Programming
    Replies: 6
    Last Post: 01-16-2005, 01:03 PM