Print Number of Iterations in a While Loop

I am writing a program that the user guesses a number that is randomly generated. They keep guessing it until it is right and I did that by a while loop. I have the code for the entire process but at the end I am trying to print how many times it took them to win.

Right now I have:


int n = 0;

if (userNum == i) {
cout << "That's right! You won! You won the game in ? tries!" << endl;
userNum = i;
}

Instead of ?, I need it to be the number of iterations it took them to win, how do I do this? 'n' is the variable I am using for number of iterations.
Hi, you can simply increment the variable 'n' each time the user takes a guess.
I don't know exactly how to do this.
Fairly simple for cout. Goes like this:

cout << "That's right! You won in " << n << " tries!" << endl;
Wait a minute... I don't understand what the problem is.

A. 'n' has the right value but you don't know how to print this value
B. you don't know how to increment 'n'
C. you have both the problems

If your issue is (A), then YFGHNG gave you an appropriate answer.
If your issue is (B), then you simply have to do n++ when it is appropriate, i.e. after the user has tried a number.
If your issue is (C), just combine the two answers.
Topic archived. No new replies allowed.