Please use variable names that provide meaning. While reading your code, I have to decipher the cryptic alphabet of variables. It would be much easier if you had variables such as guessed_input, secret_number, upper_limit, etc.
How do I store the numbers so the computer won't guess a number higher/lower than the stored numbers? |
Take a look at your random number generation formula
a = (1 + rand() % b);
The integer modulus gives you a number in the range [0, divisor-1]. The +1 fixes the range to be [1, divisor]
The range you are interested in is [lower, upper] where lower is changed every time the computer guesses too low and upper is changed every time the computer guess too high.
Take a moment to count how many numbers there are when you start at 1 and go to 10. 10 right? 10 - 1 + 1 = 10
In general last - first + 1 is the formula for counting how many integers are in [first, last].
So we know that rand() % something will be [0, something-1]
Now try to incorporate that into creating a new random formula:
(upper - lower + 1) is the number of integers in [lower, upper]
rand() % (upper - lower + 1) will be in the range of [0, upper-lower+1-1]
lower + (rand() % (upper - lower + 1)) is the range [lower, upper]
Try to imagine if I had used the alphabet for variables during that explanation, and picture how much longer that it would take you to understand what I was trying to say. The letters u and l could have been fine for upper and lower in this situation, but in general people use longer names unless it is a math formula or a for loop counter.