count keeping

Hi all,

Can someone help me with the command to keep count of the guesses that the user makes and display the number of guesses.


#include "stdafx.h"
#include <cstdlib>
#include <time.h>
#include <iostream>
using namespace std;



int main()
{

srand(time(0));

int number;

number = rand() % 800 + 1;

int guess;

do
{

cout << "Enter your number: ";
cin >> guess;

if (guess < number)

cout << "Your number is less than the secret number" << endl;

else if (guess > number)

cout << "Your number is more than the secret number" << endl;

else

cout << "Your guess is correct!" << endl;

}
while (guess != number);

system("PAUSE");

return 0;

}
It's really quite easy. Just make an integer variable that counts the number of times you go through the do-while loop.
Thanks for your help, i've create an "int count"; should i put the statement right after or before the while statement; can you give an idea on which way to go.

You will want to initialize your count variable outside the loop and increase it each pass through the loop.

for example

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
int numberOfGuesses=0;

do 
{
     cout << "Enter your number: ";
     cin >> guess;

     if (guess < number)
     {
           cout << "Your number is less than the secret number" << endl;
            numberOfGuesses++;
      }
     else if (guess > number)
      { 
           cout << "Your number is more than the secret number" << endl;
           numberOfGuesses++;
       } 
       else
       {
           cout << "Your guess is correct!" << endl;
           cout<<"You guessed in "<<numberOfGuesses<<" tries!";
        }
} 
while (guess != number);

system("PAUSE");

return 0;

}



Brendon Glanzer
UAT strudent
i added the lines below and the program works now; Thanks u very much.

srand(time(0));
int number;
number = rand() % 800 + 1;
Topic archived. No new replies allowed.