I'm new to C++ and have been working through Michael Dawson's 'Beginning C++ Programming'. What I've been reading thus far seems to make sense, I've been working through the examples and I think he explains them in the book rather well.
My problem though is with the exercise he sets, because he doesn't provide any answers, which I always find frustrating when reading any text book.
I had previously left this question but I'm going back over it now and still cannot get it right. For those who have the book its the exercise at the end of Chapter 2 where he asks you to go back over the Guess My Number program, and change it so that the computer guesses a number which you input.
I thought I'd make this simple to start with and simply pick a number between 1 and 10, and have the computer keep generating a number between 1 and 10 using a 'while' loop until it got it right. My problem though is that it will not get out of the while loop, and just keeps generating a random number over and over, even if it chooses the correct number. I also tried a "do {statement} while (guess != theNumber)" loop, but i'm gettin same infinte loop problem.
I fear I've made a simple error... but I cannot spot it. This is what I have written so far:
cout << "Enter a number between 1 and 10: ";
cin >> theNumber;
srand(time(0)); // seed random number generator
int guess = rand() % 10 + 1;
cout << "\nComputer guessed number " << guess << ".";
while (guess != theNumber)
{
cout << "\nGuess " << tries << " was wrong.";
++tries;
int guess = rand() % 10 + 1;
cout << "\n\nComputer guessed number " << guess << ".";
}
cout << "\nThe computer got it! It took the computer " << tries << " guesses!\n";
return 0;
}
After I had sorted this program, I was then going to guess a number between 1 and 100 and try and manipulate the random number generator of the computer so that it honed in on the number quicker then it currently does. Every attempt I had at that simply crashes the program. I was thinking something along the lines of "int guess = rand() % guess + 1" but this is crashing the program.
I've held off asking for help because I wanted to figure it out myself, i'm slowly going insane though, I've got harder programs then this working and its bugging me... excuse the pun.
Okay your problem is pretty simple. Your condition is if guess != theNumber, right? Well... you never change the value of guess.
int guess = rand() % 10 + 1;
Having that in your loop doesn't actually change the value of guess, it actually creates a new variable called guess... take out the 'int' and it will work just fine.
You see, because you've got brackets {} you've created a new scope... and a new variable in that scope (not the old one that is being checked). To learn more about scope read here: http://msdn.microsoft.com/en-us/library/b7kfh662(VS.80).aspx
To stop it from guessing the same number twice, decrease the modulus value by one each guess, and keep each number it guesses in an array. Then, check the array to see if it has guessed the number it comes up with before. If it has guessed the number before, add 1 to it, and check if the resulting number has been guessed. If you get to the highest number while checking, go back to 1.
Also, what happens if I put in 11? The computer is stuck guessing forever! Add this code where the user inputs a number:
1 2 3 4 5 6 7
int theNumber = 0;
do
{
cout << "Enter a number between 1 and 10: ";
cin >> theNumber;
}
while(theNumber < 1 || theNumber > 10);