0

Computer guesser. Still need help?

I am trying to input a number and have the computer guess it through a series of random guesses. I am sure there is a much better way to do it but I am tasked with doing it with the little knowledge I have so far. Can anyone give me pointers? It just keeps saying "out of time" Is it because of sololearn's IDE? Does my code look like it should work? I don't care if theres a better way or a more efficient way I just want to know if what I did should work and if it shouldn't why doesn't it? https://code.sololearn.com/cQMwNgzD9Oej/#cpp

5th Jun 2017, 5:57 PM
Bryan
2 odpowiedzi
0
your newguess var counts only once at the beginnig. you should generate random number each time computer makes a mistake
5th Jun 2017, 6:43 PM
denis
denis - avatar
0
First of all your guess is done in the wrong place. You're taking a guess before you enter the do while loop and after that no more. Where do you think the computer gets the next guess from? Not from newguess, newguess has the same issue. What you want is to do the guess right after you enter the do while loop, this way you get a new number every iteration. Next: upper = 100, lower = 0; rand()%(upper-lower)+lower; For example generates a number from 0 to 100(exclusive) so if the number was 100, you'd never find it. You'd want to add 1 to upper to make it 100(inclusive) like: rand() % (upper + 1 - lower) + lower; On to the next error: Do you think the if(guess > number) and if(guess < number) is ever evaluated? Unless guess is the same as the number in the first guess, no. Why? Spoiler: These ifs are inside if(guess == number) All right, I don't want to rush things too much. If you make an attempt on that I can show you some more :) Just reply here and I'll continue ^^.
5th Jun 2017, 6:53 PM
Dennis
Dennis - avatar