For loops on Guessing game?

I'm making a program in where you are trying to guess the computer's random number, but I want to restrict it with having only 5 chances to guess the right number using a for loop. Also I want to add an output that says That you lost after the 5 guesses are used.
This is my code so far.

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
59
60
61
62
63
64
 #include<iostream>
#include<string>
#include<cstdlib>
#include<time.h>


using namespace std;

int main()
{
    int number; // The correct number
    int guess;  // The user's guess
    bool correct = false; // If the user has correctly guessed the number
    int numGuesses; // The number of times the user has guessed

    // Seed the random generator.
    srand(static_cast<int> (time(NULL)));
	
    // Generate a random number between 1 and 100.
    number = (rand() % 100) + 1;

    
    //////////////////////////////////////////////
    // Start modifying program here

    // This is the loop from the original program, where the user 
    // is allowed an unlimited number of guesses.
    // Change this to a for loop that only allows 5 guesses
    // Add any additional variables you need to the variable list
    // at the top of the program. You can also delete variables
    // that are not needed with the for loop.


	while(!correct)
    {
        cout << "guess the number the computer randomly picked between 1 - 100: ";
        cin >> guess;

        // Check if the user has guessed the correct number.
        // If not, tell him if his guess is too low or too high
        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;
            
            // Update the looping condition so the program will exit the loop
            correct = true;
        }
	}	
}
    // End of program modifications
    //////////////////////////////////////////////

    system("pause");
	
    return 0;
}


How do I add the for loop in order to make it work.
I'm guessing that I'll need, but I'm not sure if it's right and/or where should I put it and what else do I need as well.

 
for (numGuesses = 0; numGuesses < 5; numGuesses++)
Line 17 there is no need to static cast it to an int. Time should already return an unsigned integer.

Basically for starters you want to define a constant variable for the allowed number of guesses const int GUESSES = 5;
Then you want to use a for loop instead of a while loop on line 34( that boolean value isn't necessary also you can just break when its true. )
To setup your for loop you would want this instead of the while
for( int i = 0; i < GUESSES; ++i ) Then as I mentioned earlier use break to exit the loop instead of the boolen so for line 54 put this

break;

To get the amount of guesses they attempted you can either use a count variable then increment each time the for loop is used. Or you can move I outside of the for loop then use i + 1 as the guesses either method should work.

1
2
3
4
5
6
7
8
9
int count = 0;

for( int i = 0; i < GUESSES; ++i )
{
    ++count;
     if( guess == answer ) break;
}

std::cout << "It took you " << count << " guesses to get it correct." << std::endl;


or

1
2
3
4
5
6
int i = 0;
for( ; i < GUESSES; ++i ) //for( i; i < GUESSES; ++i ) if you prefer
{
    if( guess == answer ) break;
}
std::cout << "It took you " << i + 1 << " guesses to get it correct." << std::endl;
This program is an assignment so it has the requirements I had given.
the code that i have is what I've done so far.
However, the thing I can't do is the for loop to allow just those 5 guesses
You said earlier you wanted a for loop not to use a while loop like a for loop.

Basically you want something like

1
2
3
4
5
6
7
8
9
int guesses = 0;
std::cout << "Please enter the allowed amount of guesses: ";
std::cin >> guesses;

while( guesses > 0 )
{
    //do stuff
    --guesses;
}
Another option

1
2
3
4
5
6
7
8
9
10
11
12
int attempts = 5;
for(int x = 0; x < 5 && guess != number; x++, attempts--)
{
    //do stuff
}

attempts++;  //if your results are off by one

if(attempts == 0)
    //output loss
else
    //output success 


Only add line 7 if your results are off by one. They shouldn't be
Last edited on
Topic archived. No new replies allowed.