### for-loop:Did i follow the directions of my assignment?

For your program you will modify the Guess Number program from the counter-controlled while loops program in the while Loops Part 1 lab. Use a for loop to allow the user up to 5 guesses. If their guess is correct use a break statement to exit the for loop.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include #include #include using namespace std; int main() { int number; int guess; bool correct = false; int numGuesses ; srand(static_cast (time(NULL))); number = (rand() % 100) + 1; for(int numGuesses = 0; numGuesses<5 && !correct;numGuesses++) { cout << "guess the number the computer randomly picked between 1 - 100:"; cin >>guess; if (number > guess) { cout << "sorry,your guess is too low" << endl; } else if (number
instead of using the correct bool variable, you could break out of the for loop when the right number is guessed. You use a return statement, so the correct variable isn't really used.

At the moment you get the "Sorry you lost " message regardless.

The "Sorry you lost " message could be another else if condition.

HTH
so that else if statement be this?

 ``12`` ``````else if (number != guess && numGuesses = 5) cout<<"Sorry,you lost.The number is:"<< number <
Last edited on
Looks alright to me - give it a try.

Hope all goes well.
Here is my code but nothing happens
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include #include #include #include using namespace std; int main() { int number; int guess; bool correct = false; int numGuesses; srand(static_cast (time(NULL))); number = (rand() % 100) + 1; for(int numGuesses = 0; numGuesses<5 && !correct;numGuesses++) { cout << "guess the number the computer randomly picked between 1 - 100:"; cin >>guess; if (number > guess) { cout << "sorry,your guess is too low" << endl; } else if (number
Sorry, wasn't thinking straight - numGuesses only makes it to 5 after the for loop, so you need to compare to 4. Good to see you changed the assignment operator to an equality operator.

This should be sufficient:

`else if (numGuesses== 4)`

I would prefer a comparison to test for a win rather than leave it for an else clause. The else can be for any other error - not that there should be any of those.

Also get rid of references to the correct variable on lines 12, 19 & 46.

And no need for the break before the return (have one or the other). If you want to have more code after the end of the for loop, then use the break.

See how you go.

I dont understand why i need to change those lines that you say.What variable?
 I dont understand why i need to change those lines that you say.What variable?

TheIdeasMan wrote:
 Also get rid of references to the correct variable on lines 12, 19 & 46.

The bool variable named correct - declaration of it on line 12.

I am saying you should get rid of that variable because it is not needed.
Topic archived. No new replies allowed.