never ending do loop

Hi i starting programming and im currently working through a book and one of the tasks asked me to make a program that has the computer guess my number heres my code and it is a never ending loop, can someone please tell me why and how to change it.

int main() {

int theNumber;
int tries = 0;
int computerGuess = rand() % 10 + 1;

cout << "\nwelcome to guess my number\n\n";
cout << " enter a number: ";
cin >> theNumber;

do
{
cout << " computer guess: " << computerGuess;
tries++;

if (computerGuess > theNumber)
{
cout << " computerguess to high \n";

}

if (computerGuess < theNumber)
{
cout << " computerguess is low \n";


}

} while (computerGuess!=theNumber);



cout << " thats it you got it! you got it in:" << tries << " guesses \n";





return 0;

}
Last edited on
You need to change computerGuess whithin the loop:
1
2
3
4
5
6
7
8
9
10
11
int computerGuess;

cout << "\nwelcome to guess my number\n\n";
cout << " enter a number: ";
cin >> theNumber;

do
{
computerGuess = rand() % 10 + 1; // Note
cout << " computer guess: " << computerGuess;
tries++;
i did this and now computerGuess is not recognised in the while() section of the code. Any suggestions?
What does "not recognised" mean?

Show the modified code.
Topic archived. No new replies allowed.