Hey Doubt,
It looks like the reason for your troubles is in the rand function on line 36. There is no reason for any of your calls to the rand function to be different.
In line 30, assuming the guessed number was 34 and was too low you then have the
rand() % (100-34)+34;
This will generate a number within 0 and 65 plus 34. That means you'll have a number between 34 and 99.
Then on line 36 you have
rand() % (max+min);
Assuming that we had the result above and the second number guessed was 89 which was to high you end up with a number between 0 and 88 + 34. This gives you a number between 34 and 122.
Hopefully from this you can see why the results may be completely bonkers haha.
It might be helpful to write the function as follows:
1 2 3 4 5 6
|
int numberGenerator(int max, int min)
{
int result;
result = rand() % (max-min)+min+1;
return result;
}
|
Using this function in place of the function called on line 34, you would have max set at 89 and min set at 34. This would give you a number in the range of 0 and 54 + 34 + 1. Which would turn out being between 35 and 89.
Note that you'll have to have min == 0 at the start of the program. You can see that it would work for the initial random function as well:
rand() % 100
Gives you a number between 0 and 99, + 1 gives a range between 1 and 100.
So if you subtract 0 from 100 you get a range of 0 and 99, add a 0 and you get 0 and 99, add the additional 1 and you get a number between 1 and 100. This way you can use the same function for every call and just keep passing the changing min and max variables to it.