Thread: Beginner HiLo game glitches?

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    1

    Beginner HiLo game glitches?

    Hello, below is my code for a HiLo game. I added the try again option, and now when you guess the right number, it still says the number is too high or too low, until you continue to guess that number. It also doesn't compute the right amount of tries. Could anyone tell me whats wrong? Thank you to anyone who can help (:

    Code:
    #include <iostream>
    #include <ctime>
    
    
    using namespace std;
    
    
    int main ()
    {
    int range;
    int num = 0;
    int guess = 0;
    char answer;
    srand(time(0));
    
    
    
    
    cout << "Hello! Welcome to the HiLo game!" << endl;
    cout << "To start, please enter the range: ";
    cin >> range;
    
    
    while (answer != 'N' && answer != 'n')
    do
        {
            int tries = 0;
            num = rand() % range + 0;
            {cout << "Enter a guess between 0 and "<<range<<":";
            cin >> guess;
                tries++;}
            if (guess > num)
                {cout << "Too high! Try again!\n\n" << endl;}
    
    
                else if (guess < num)
                {cout << "Too low! Try again!\n\n" << endl;}
    
    
                    else
                    {cout << "\nCorrect! It took you  " << tries << " tries!\n";
                    cout << "Would you like to play again? (Y for yes N for no)";
                    cin >> answer;}
    
    
                if ( guess == -999)
                    {cout << "The correct number was:" << num << endl;}
    
    
                {if ( guess > range || guess < 0)
                    {cout << "Error: invalid guess, try again." << endl;
                    tries--;}}
                }
            while (guess != num);
    
    
    system("pause");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > num = rand() % range + 0;
    Perhaps you should have a constant random number, rather than changing it every time the user makes a guess.
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HiLo Game
    By LearnOnTheFly in forum C Programming
    Replies: 13
    Last Post: 03-13-2012, 09:28 PM
  2. CPU glitches solved
    By VirtualAce in forum General Discussions
    Replies: 52
    Last Post: 03-29-2011, 07:48 AM
  3. Hey guys help with HiLo program
    By icehead14 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2010, 12:00 PM
  4. Graphical Glitches
    By Pressure in forum Tech Board
    Replies: 3
    Last Post: 04-27-2005, 07:42 AM
  5. Color Glitches in Console Prog.
    By Darkflame in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2002, 11:52 PM