how do I print out the amount of times a program has looped.

Ok so my program is supposed to ask the user for a number then generate random numbers until it(the oracle) guesses my number. After the oracle guesses my number, i need to print out the number of tries it took the oracle to guess my number. So far my program loops numbers and guesses mine, i just cant figure out how to print out the number of guesses it took the oracle to guess my number. Here is what i have;
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
  4 #include <iostream>
  5 using namespace std;
  6 #include "Random.h"
  7 
  8 int main()
  9 {
 10   int variable;
 11 
 12   cout << "What is your secret number between 1 and 100?" << endl;
 13   cin >> variable;
 14 
 15 Random oracle(1,100);
 16 int oracle_guess = 0;
 17 
 18  while(oracle_guess != variable)
 19  {
 20    oracle_guess = oracle.get();
 21    cout << oracle_guess << endl;
 22  }
 23 
 24 
 25 
 26 if(oracle_guess == variable)
 27 {
 28   cout << "Your Number is: " << oracle_guess << endl;
 29 
 30 
 31 }
 32 
 33 
 34 return 0;
 35 }
1
2
3
4
5
6
7
8
9
10
int count = 0;

while(oracle_guess != variable)
{
   oracle_guess = oracle.get();
   cout << oracle_guess << endl;
   ++count;
}

cout << "It took " << count << " guesses.";
Last edited on
Have a value. Set it to zero at the start. Add one to it each time the oracle guesses. Output the value at the end.
Topic archived. No new replies allowed.