Thread: Else if noobie trouble

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Else if noobie trouble

    Hi,

    I am trying to create a game where a random integer between 1 and 10 is created and if the player guesses correct it says "win", if they guess too low it says "Too low, if they guess to high it says "Too high" BUT if they guess out of range it says "invalid guess".

    My problem is that when the player enters a number within range, the last part of my If Else statement is still running (the invalid guess).

    Code:
      
        if (guess_number == random_integer)
        {
        cout << "You win! ";
        }                 
        else if (guess_number < random_integer)
        {
        cout << "Your guess was too low ";     
        }
        else if (guess_number > random_integer)
        {
        cout << "Your guess was too high";     
        }
        else ( (guess_number < 1) || ( guess_number > 10) );
        {
        cout << "invalid guess, needs to be between 1 and 10";
        }
    I need it to not run the last ELSE if the guess is within range.

    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hint: reorder your statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I changed indentation.
    Now you should be able to see what's wrong
    Code:
        if (guess_number == random_integer) {
            cout << "You win! ";
        }
        else 
            if (guess_number < random_integer) {
                cout << "Your guess was too low ";     
            }
            else 
                if (guess_number > random_integer) {
                    cout << "Your guess was too high";     
                }
                else 
                    ( (guess_number < 1) || ( guess_number > 10) );
        {
            cout << "invalid guess, needs to be between 1 and 10";
        }
    Kurt

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    else has no conditional, it is implicitly "everything else" (hence the name).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thank a lot! Is this a precedence issue?

    Will look at that


  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Code:
        if (guess_number < random_integer)
        {
        cout << "Your guess was too low ";     
        }
        else if (guess_number > random_integer)
        {
        cout << "Your guess was too high";     
        }  
        else if (guess_number == random_integer)
        {
        cout << "You win! ";
        }
        else if ( (guess_number < 1) || ( guess_number > 10) )
        {
        cout << "invalid guess, needs to be between 1 and 10";
        }
    Thanks everyone, the above is working.

    Very kind.
    Last edited by Swerve; 11-18-2007 at 05:59 AM.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Hint: reorder your statements.
    I think the point was that you should check if you have valid input as the first thing not the last.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Swerve View Post
    Thanks everyone, the above is working.
    Very kind.
    Don't think so
    Code:
        if (guess_number < random_integer)
        { 
        cout << "Your guess was too low ";     
        }
        else if (guess_number > random_integer) 
        {
        cout << "Your guess was too high";     
        }  
        else if (guess_number == random_integer)
        {
        cout << "You win! ";
        }
        else if ( (guess_number < 1) || ( guess_number > 10) )  // can never be true
                                                                // it's either lower greater or equal
        {
        cout << "invalid guess, needs to be between 1 and 10";
        }
    Kurt

  9. #9
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks Zuk and everyone!

    Now I am trying to limit the player to 3 guesses, but the IF ELSE's are looping for ever:-

    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int human_guess = 0;
        int guess_count = 0;
        int random_integer;
        srand((unsigned)time(0));
    
        
        random_integer = (rand()&#37;10);
        
    
        cout << "Hi and welcome to the Guess the Number game.\nThe BLAH BLAH BLAH ur guess and then press Enter. Good luck! ";
    
    
        while (guess_count < 3)
        {
            guess_count ++;
            cin >> guess_count;
            
    
    
            if (human_guess < random_integer)
            {
                cout << "Your guess was too low " << endl;
        
            }
    
            else if (human_guess > random_integer)
            {
                cout << "Your guess was too high" << endl;
     
            }
    
            else if (human_guess == random_integer)
            {
                cout << "You win! " << endl;
     
            }
    
            else if (human_guess < 1)
            {
                cout << "invalid guess, needs to be 1 or higher" << endl;
    
            }
            else if (human_guess > 10)
            {
                cout << "invalid guess, needs to be 10 or less" << endl;
    
            }
            else if (human_guess == 3)
            {
                cout << "you have ran out of guesses" << endl;
            }
            
                cin.get();
    
        }
    
      
        cin.get();
    
    }
    Could I get a point in the right direction please.

    Many thanks
    Last edited by Swerve; 11-18-2007 at 08:44 AM.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Youre doin' it again.
    You can't just add any number of else if statements
    In your most recent code the last 3 if blocks can never be reached.
    Kurt

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly you need to realize that if there is a possibility of invalid input, you must check this first.

    Code:
        int human_guess = 0;
        int guess_count = 0;
        //...
    
        while (guess_count < 3)
        {
            guess_count ++;
            cin >> guess_count;
    
            if (human_guess < random_integer)
    You never get any input to human_guess, instead you get input for the counter that is supposed to be managed by your code.

    You need to input into human_guess. There should be exactly one input statement in the loop and the loop should take care that it is called at least 3 times (more if you don't count invalid input).

    Actually I wouldn't let the code get any further from the input statement until user has entered acceptable input. For that you'll need another loop that runs until user has entered valid choice.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM