You are using a version without Ads of this website. Please, consider donating:

### 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;
 123456789101112131415161718192021222324252627282930313233 4 #include 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 }
 12345678910 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.