-1

I am confused on how to add a counter to my code - I'm writing a 2 player number guessing game and I want to add the number of guesses it took to satisfy the program.

This is what I've got so far:

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{

    int x;
    int  guess;

    std::cout << "Enter the number for the player to guess.";
    std::cin >> x;

    do
    {
        std::cout << "Enter your guess.";
        std::cin >> guess;
        if (guess > x)
            std::cout << "lower\nEnter your guess.\n";
        else if (guess < x)
            std::cout << "higher\nEnter your guess.\n";
        else
            std::cout << "You guessed it!\n";
    } while (guess != x);

    return 0;
}
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Sneha
  • 15
  • 1
  • 1
  • 3

4 Answers4

2

You just need to add a variable and increase it each time you loop, like so:

int guesses = 0;

do
{
    guesses++;
    std::cout << "Enter your guess.";
    std::cin >> guess;
    if (guess > x)
        std::cout << "lower\nEnter your guess.\n";
    else if (guess < x)
        std::cout << "higher\nEnter your guess.\n";
    else
        std::cout << "You guessed it!\n";
} while (guess != x);

std::cout << "You took " << guesses << " guesses!" << std::endl;
Kvothe
  • 1,819
  • 2
  • 23
  • 37
  • Thanks Kvothe, I have so much to learn, but I'm getting there, two weeks ago I felt like a hacker because I log'on to a command line server, lol... – Sneha Apr 18 '15 at 19:26
0
  #include <iostream>
#include <cstdlib>
#include <ctime>


 int main()

{

int x;
int  guess;

std::cout << "Enter the number for the player to guess.";
std::cin >> x;
int count = 0;    

  do
{
    std::cout << "Enter your guess.";
    std::cin >> guess;
    if (guess > x)
        std::cout << "lower\nEnter your guess.\n";
    else if (guess < x)
        std::cout << "higher\nEnter your guess.\n";
    else
        std::cout << "You guessed it...";
    count++;
  } while (guess != x);
  std::cout << "in " << count << "times" <<endl; 

  return 0;
 }

The count variable will count the number of times the player guess and you can print the count after guess is correct.

cosmos
  • 2,263
  • 1
  • 17
  • 30
0

Use a variable to store the count of guesses and increment it until the answer matches. :)

#include <iostream>
#include <cstdlib>
#include <ctime>


 int main()

{

int x;
int  guess;

std::cout << "Enter the number for the player to guess.";
std::cin >> x;
int counter=0;
  do
{
    counter++;
    std::cout << "Enter your guess.";
    std::cin >> guess;
    if (guess > x)
        std::cout << "lower\nEnter your guess.\n";
    else if (guess < x)
        std::cout << "higher\nEnter your guess.\n";
    else
        std::cout << "You guessed it!\n";
  } while (guess != x);
std::cout<<"You have guessed "<<counter<<" times for correct answer"<<endl;
  return 0;
 }
Udit Kumawat
  • 654
  • 1
  • 8
  • 21
0
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{

    int x;
    int guess;

    std::cout << "Enter the number for the player to guess.";
    std::cin >> x;

    int counter = 0; //make a new variable to count the guesses
    do
    {
        std::cout << "Enter your guess.";
        std::cin >> guess;
        if (guess > x)
            std::cout << "lower\nEnter your guess.\n";
        else if (guess < x)
            std::cout << "higher\nEnter your guess.\n";
        else
            std::cout << "You guessed it!\n";
        ++counter; //increase it each time a guess happened
    } while (guess != x);

    //print the count of guesses
    std::cout << "Took you " << counter << " guesses!" << std::endl;

    return 0;
}
NaCl
  • 2,683
  • 2
  • 23
  • 37