0
Does this program work as supposed to?
I was having an issue with choosing a random number in a range between ranges. For example 5-25. The code does not run as specified on my phone and I do not have access to a computer to run it. I know there is a lot of room for improvements to my code but I am just trying to test if I have the appropriate ideas and syntax to complete this problem as opposed to run time and algorithm "fill in the blank" (I can't think of a word lol. Problem taken from cplusplus.com https://code.sololearn.com/cV8r6v5fsE9d/?ref=app
2 odpowiedzi
+ 1
I'd use two variables low and high. Move them after each guess as required.
int low = 1, high = 100, compGuess = rand()%(high-low+1)+low;
if (userInput.compare("Too high"))
high = compGuess-1;
else if (userInput.compare("Too low"))
low = compGuess+1;
compGuess = rand()%(high-low+1)+low;
+ 2
Thanks a ton. I forgot that I could just add variables to make it more efficient. I was caught up in the equation and making sure I had the rand numbers correct.