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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<string>
#include<cstdlib>
#include<time.h>

using namespace std;

int main()
{
int number;
int guess;
bool correct = false;
int numGuesses ;

srand(static_cast<int> (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 <guess)
{
cout <<"sorry, your guess is too high" << endl;

}
else
{
cout << "you guessed right, you win!" << endl;
correct= true;
system("pause");
return 0;

}

}
cout<<"Sorry,you lost.The number is:"<< number <<endl;
system("pause");
return 0;



}
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?

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

Hope all goes well.
Here is my code but nothing happens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<string>
#include<cstdlib>
#include<time.h>

using namespace std;

int main()
{
    int number;
    int guess;
    bool correct = false;
    int numGuesses;
    
    srand(static_cast<int> (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 <guess)
        {
            cout <<"sorry, your guess is too high" << endl;
            
        }
        else if (numGuesses== 5 && number != guess)
        {
            
            cout<<"Sorry,you lost.The number is:"<< number <<endl;
        }
        
        else
        {
            cout << "you guessed right, you win!" << endl;
            correct= true;
            break;
            return 0;
            
        }
        
    }
    
   
    
    
    
}
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.