random number while loop

Hello there! I am working on a project that will allow the user to input a "guess" (a number) in the range of 1-100. I have created a while loop that will narrow the correct "guess" (number) down to the random number the computer has chosen. However, when I run the program, the guess and other "cout" statements repeat over and over.

Any advice is greatly appreciated!


#include <iostream>
#include <conio.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
int guess, number;

cout << "Try to guess a number between 1 and 100" << endl;
cout << "Press a key to begin" << endl;
getch();
srand(clock());
number = rand()%100 + 1;

cout << "What is your guess? ";
cin >> guess;

while (guess >= number || guess <= number)
{
if (guess > number)
cout << guess << "is to high. Try again. ";
cout << "What is your guess? ";

if (guess < number)
cout << guess << "is to low. Try again. ";
cout << "What is your guess? ";

if (guess == number)
cout << guess << "is correct! you got it in ? guesses!";
}

getch ();
return 0;
}



The final outcome should like similar to this:

Try to guess a number between 1 and 100
Press a key to begin
What is your guess? 78
78 is too high. Try again.
What is your guess? 12
12 is too low. Try again.
What is your guess? 45
45 is too high. Try again.
What is your guess? 32
32 is too low. Try again.
What is your guess? 38
38 is correct! You got it in 5 guesses!
Hi maclewis,

your while loop doesn't terminate because
(guess >= number || guess <= number)
is allways true.
You can solve this by altering the statement, so that the while loop is terminated ( or not continued ) if guess = number.

You also should keep in mind that an if-statement only regards to the next command or block.

If you want to put out the number of guesses you could use a variable as a counter, initialising it with 0 and increment it everytime a guess is made.

You could also try to use a do-while loop instead of a while loop to avoid prompting the user for input at more than one point.

Beside that: Please indent to make your code easier to read and use code tags.

Hope this helps you a bit.

Greatings
Last edited on
You could also use a do-while;

example
1
2
3
4
5
6
7
8
9
do 
{

......


} while ( guess != number );


#include <iostream>
#include <conio.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
int guess, number, end; // end added to end program

cout << "Try to guess a number between 1 and 100" << endl;
cout << "Press a key to begin" << endl;
getch();
srand(clock());
number = rand()%100 + 1;

cout << "What is your guess? ";
cin >> guess;

while (guess >= number || guess <= number)
{
if (guess > number)
{
cout << guess << "is to high. Try again. ";
cout << "What is your guess? ";
cin >> guess; // added to have the user guess again
}
else; // added to prevent run-time error
if (guess < number)
{
cout << guess << "is to low. Try again. ";
cout << "What is your guess? ";
cin >> guess; // added to have the user guess again
}
else; // added to prevent run-time error
if (guess == number)
{
cout << guess << "is correct! you got it in ? guesses!, press any key to terminate program";
cin >> end; // added for input from user
return 0; // added to end program
}
}
getch ();
return 0;
}

I don't know if this helps much, I am fairly new to C++ myself and was wanting to take on a challenge. The first thing I did was and else statements in your while loop. The next thing I did was add brackets for your if statements, if i recall they can only run one statement or line of code without brackets. But by all means I am learning as well you could probably polish up what I have shown you.
Ok I worked on your program and I have it save on my desktop do you want me to post it and you can look up all the stuff I chanced or I can just keep trying to help you through words.. Because I learn better by seeing example...
Yes, I would very much like to see what you have changed within the program. I learn better by seeing my mistakes as well as new ways to run the same program.

Thanks for the help so far everyone!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cmath>  // probably better not to use all these C libraries and use C++ ones but I'll keep them here for your sake
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{
   int guess, number; // end removed

   cout << "Try to guess a number between 1 and 100" << endl;

   srand(clock());   // you could also use: srand(time(0)); 
   number = rand()%100 + 1;

   int tryCount=0; // so we can tell user how many guesses/tries

   do{
      tryCount++;

      cout << "What is your guess? ";
      cin >> guess;
      if (guess  > number)
         cout << "Smaller" << endl;
      else if (guess < number)
         cout << "Larger" << endl;
      else
         cout << "Congrats! you got it in: " << tryCount << " guesses." << endl;

      } while (guess != number) ;

   cin.ignore(1000,'\n');  // if you need a pause...most ide's have an option to keep console open
   return 0;
}
Last edited on
Here the same program with your while loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main( )
{

    int guess, number, counter = 0;

    srand( time( NULL ) );

    number = ( rand( ) % 100 ) + 1;

    cout << "Try to guess a number between 1 and 100\n" << endl;

    cout << "What is your guess?\n";

    cout << endl;

    while( guess != number )
    {
        cout << endl;

        cout << "> ";
        cin >> guess;

        cout << endl;

        if( guess > number )
        {

            cout << guess << " is to high. Try again.\n";

        }

        if( guess < number )
        {

            cout << guess << " is to low. Try again.\n";

        }

        // Everytime the loop, loop through its going to add one to the counter.
        counter++; // counter = counter + 1;

        if( guess == number )
        {

            cout << guess << " is correct! you got it in " << counter << " guesses!, press any key to terminate program\n";
            break; // this is going to break out of the loop.

        }
    } // end while

    return( 0 );
}



Last edited on
the break is not necessary, neither is the conditional
if( guess == number )
because the while loop already has this check simply having line 52 at the bottom outside the loop would have sufficed.

also it's best to use else/if declarations rather than if, if, if reason being it's less computations
Having to only check 1-n using if/else if/else, and having to check n times using if if if

also the program terminates straight after this output: " press any key to terminate program "

also return 0; works (hint* it's not a function with parentheses) as does: return ((((( 0 )))));
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main( )
{

    int guess, number, counter = 0;

    srand( time( NULL ) );

    number = ( rand( ) % 100 ) + 1;

    cout << "Try to guess a number between 1 and 100\n" << endl;

    cout << "What is your guess?\n";

    cout << endl;

    while( guess != number )
    {
        cout << endl;

        cout << "> ";
        cin >> guess;

        cout << endl;

        // Everytime the loop, loop through its going to add one to the counter.
        counter++; // counter = counter + 1;

        if( guess > number )
        {

            cout << guess << " is to high. Try again.\n";

        }
        else
            if( guess < number )
            {

                cout << guess << " is to low. Try again.\n";

            }
        else
            if( guess == number )
            {

                cout << guess << " is correct! you got it in " << counter << " guesses!, press any key to terminate program\n";
              

            }
    } // end while

    return( 0 );
}



I still don't get why it works without the break in it... But I understand why you have to use the else it makes the program a little bit faster...
because the line 49 condition is true, which means the while loop condition line 23 is false...
the while loop never starts again, line 52 is executed and that's the end of it.

you should also use endl; at the end of your output to flush the stream, rather than '\n'.
or you can also use: cout << flush;

This depends on what your doing, if you have multiple outputs that will be executed '\n' is fine, but if you have output that may only happen once. Such as the case if the user guessed the right number first try, then the buffer should be flushed.
Last edited on
WOW..... I'm most be blind and I wrote this lol..... and thanks for the tip with the flush I will be looking into it.
Topic archived. No new replies allowed.