### guess a random number (I'm having trouble with loops)

Hey in my beginners C++ class we have to create a programs that generates a random number and the user needs to guess the number. Heres the assignment:-

 Note: This program should use a while loop. Write and test the program guessNumber for a simple number guessing game that allows the user to guess a number between 0 and 100. The game will start by selecting a random number for the user to guess. See the example program under Examples that demonstrates the use of a random number. Each time the user guesses a number, the game will respond whether the guess is too high or too low along with the range of values for the next guess. Notice that the prompt includes the guess number and that the range in the prompt changes. If the number is out of range, an error message is printed. At the end, the number of valid and invalid guesses are displayed. The following is an example of input and output: Input is shown in blue Guess 1 : Enter a number between 0 and 100 >> 60 60 is too high Guess 2 : Enter a number between 0 and 59 >> 20 20 is too low Guess 3 : Enter a number between 21 and 59 >> 16 16 is not between 21 and 59 Guess 3 : Enter a number from 21 and 59 >> 40 40 is correct It took you 3 valid guesses to find the number You had 1 out of range guess
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include using namespace std; int main () { int random; int num; int max; int min; int outofrange; int rightguess; rightguess = 1; outofrange = 0; max = 100; min = 0; srand(time(NULL)); cout << "random number: " << rand()%10 << endl; cout << "Enter an interger that is between 0 and 100: "<< endl; cin >> num; while (num != rand()%10) { if ( num > rand()%10) { cout << num << " is too high" << endl; max = num - 1; } else { cout << num << " is too low" << endl; min = num + 1; } cout << "Enter an interger that is between " << max << " and "<< min << endl; cin >> num; if (num > max || num < min) { cout << "the interger you enter is not within range, enter a number between " << max << " and " << min << endl; cin >> num; outofrange = outofrange + 1; } else rightguess = rightguess +1; } cout << " It took you " << rightguess << " valid guesses to find the number " << endl; cout << " You had " << outofrange << " out of range guess " << endl; return 0; }``````

That is my code. I'm not sure what I did wrong. But lets say the random number is 7, I guess 7 and it will not work. I'm not sure what the problem is. At first I just set a number to 67 and had someone guess that number and the program works fine. But when I code to get an actually random number it does not seem to work.
For example this worked fine:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include using namespace std; int main () { int random; int num; int max; int min; int outofrange; int rightguess; rightguess = 1; outofrange = 0; random = 67; max = 100; min = 0; cout << "Enter an interger that is between 0 and 100: "<< endl; cin >> num; while (num != random) { if ( num > random) { cout << num << " is too high" << endl; max = num - 1; } else { cout << num << " is too low" << endl; min = num + 1; } cout << "Enter an interger that is between " << max << " and "<< min << endl; cin >> num; if (num > max || num < min) { cout << "the interger you enter is not within range, enter a number between " << max << " and " << min << endl; cin >> num; outofrange = outofrange + 1; } else rightguess = rightguess +1; } cout << " It took you " << rightguess << " valid guesses to find the number " << endl; cout << " You had " << outofrange << " out of range guess " << endl; return 0; }``````

Maybe I could get one of your guys aim or something so I can get direct feedback while I'm coding.
I would ask my teacher but he takes like 48 hours to respond.
I also have a harder assignment that I would like some feedback/tips on.-
 Note : This program should use a do-while loop and for loops. Write the program printShapes to repeatedly ask the user for a character and then print output according to the following table. Your program should continue until the user enters 'f'. Note that for each valid input (a through e), at least 2 MORE inputs are needed before the program can determine what to output. The "output" column in the following table gives an example of one set of input for each valid input choice. Input character Additional required input Output If: a 1. character to print, 2. number of lines to print, 3. maximum number of characters per line Example: if character = '*', number of lines = 3, maximum number of characters per line = 4, output is **** **** **** If: b 1. character to print, 2. number of lines Example: if character = '+', number of lines = 3, output is + ++ +++ If: c 1. character to print, 2. number of lines Example: if character = '#, number of lines = 5, output is ########## ######## ###### #### ## If: d 1. character to print, 2. parallelogram height in lines Example: if character = '#, parallelogram height in lines = 5, output is ## #### ###### ######## ########## ######## ###### #### ## If: e 1. character to print, 2. maximum characters per lines Example: if character = '*', maximum characters per line = 5, output is * *** ***** *** * Example for 'e' with even number: if character = 'E', maximum characters per line = 6, output is EE EEEE EEEEEE EEEE EE If: f none (no output, this signals that the user wished to exit) any character other than a..f none error message**

I spent hours today working on the pseudo-code I thought I got part of the program right but it turned out to be wrong.
I would post my code for "a" and "c" but it makes the post even longer and they turned out to be wrong. I cant for the life of me figure out how to increase the number of characters every line like in "b" "d" and "e".
Any tips would be greatly appreciated
In the above code you always calculate the random-number anew by calling the rand()-function.

Just edit line 16 of your second code to

 `` `` ``random = rand()%100;``

And, in order to make the random number random, you should use srand() as discribed here:
http://www.cplusplus.com/reference/clibrary/cstdlib/srand.html

int main
Last edited on
Topic archived. No new replies allowed.