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!
#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.
#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>
usingnamespace 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;
elseif (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;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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 );
}
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 )))));
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.