| maclewis (9) | |
|
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! | |
|
|
|
| Tarson (2) | ||
|
Hi maclewis, your while loop doesn't terminate because
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
|
||
| OnymousIllusion (82) | |||
|
You could also use a do-while; example
| |||
|
|
|||
| Slambofett (5) | |
|
#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. | |
|
|
|
| OnymousIllusion (82) | |
| 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... | |
|
|
|
| maclewis (9) | |
|
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! | |
|
|
|
| gcampton (859) | |||
| |||
|
Last edited on
|
|||
| OnymousIllusion (82) | |||
Here the same program with your while loop
| |||
|
Last edited on
|
|||
| gcampton (859) | |
|
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
|
|
| OnymousIllusion (82) | |||
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... | |||
|
|
|||
| gcampton (859) | |
|
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
|
|
| OnymousIllusion (82) | |
| 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. | |
|
|
|