How to count number of tries of the user

Ok, so I made this program. It is working just fine. But I don't know how to make it calculate the number of tries i took the user to guess the correct number. I'm doing this practice exercise.

I have introduces an integer "tries" but I dont know how to use it now.


Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.
If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.
The program must let the user continue to guess until the user correctly guesses the number.

★ Modify the program to output how many guesses it took the user to correctly guess the right number.



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
 #include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{

    int number = rand() % 100;
    int guess;
    int tries;

    cout << "Guess the number from 1 and 100.\n";

    while(true)
    {
        cin >> guess;

        if (guess < 0)
        {cout << "Too low.\n";}

        else if
        (guess > 100)
        {cout << "Too high.\n";}

        else if (guess == number)
        {cout << "Your guess is correct.\n";}

        else
        {cout << "Your guess is incorrect. Please try again.\n";}
    }


    return 0;
}
Last edited on
Topic archived. No new replies allowed.