Well let me start by showing off my source.

//This is a game called "The Guessing Game"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
      int number;
      unsigned seed = time(0);
      srand(seed);
      seed = 1 + rand() % 100;
      cout << "Please try to guess a number from 1-100: "; 
    
   
   while(number != seed)
     { 
      cin >> number;
      if(number < 1 || number > 100)
         cout << "\nNumber is not between 1-100. Try again: ";  
      
      else if(number > seed)
         cout << "\nNumber is too high! Try again: ";
      
      else if(number < seed)
         cout << "\nNumber is too low! Try again: ";
     }
        cout << "\nThe number was: " << number << endl;
        cout << "\nCongratulations! You won the game!\n\n";
          
   system("pause");
   return 0;
}

The only thing I need is after the "The number was" statement, I want to cout something else that says how many times the loop executed. So do i need to define a variable for the loop counter? What would be the function that counts the loops?? Thanks in advance!!

You need an iterator (A variable that you can increment in the loop) ... Define a variable such as i right above the while statement. Then, for each iteration of the while loop, increase the value by one. When you come out of the loop, its value will be the number of times you ran the loop, since you increased it by one each time the loop went through.

I have here an example in my book that says:

int num = 1; //Initialize the counter
while (num <= 10)
{
cout statement
num++
}

Is this what I need to do?? But what do I put instead of the 10 there??

In the example you provided, num counts the number of times the loop runs. That's exactly what you need.

However, in the example, the condition for the whole loop (what's inside the parentheses) tells when to stop the loop, in this case, when num is over 10, or when the loop ran more than 10 times.

In your case, the number of times the loop runs has nothing to do with when to break out of the loop, so you don't need to change your while conditional.

Learning from examples is a great way to understand things; only if you understand the example completely before trying to fit your crude analogies.

And when you are not able to construct a basic loop, a quick look-up on to your text book wont do harm.

I would suggest you to use a for loop instead as it better suits your need ( ignore this advice if you are doing this problem as a homework given from a teacher with instruction as ``you HAVE TO you a while loop")

Here, a link about for-loop.

I would suggest you to use a for loop instead as it better suits your need ( ignore this advice if you are doing this problem as a homework given from a teacher with instruction as ``you HAVE TO you a while loop")

Here, a link about for-loop.

How would I set up the "For" loop in this case??

For (count = 1; number != seed; count++ ) ??

How would I set up the "For" loop in this case??

For (count = 1; number != seed; count++ ) ??

Did you tried it?
Hint: Yes, you are on the right path. Just go on... ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.