Well Ivan had the example from two players but I automatically assumed that the computer would ask the user to guess a number from a randomly generated number. In Pascal you have to first make sure you call Random or the numbers generated will not have sufficient entropy. So I googled the C++ equivalent and it gives similar warnings about the number not being random enough. Requires #include <>cstdlib
#include
using namespace std;
#include <>cstdlib
int main()
{
int ARandomNumber = rand();
int AGuess;
cout << “The computer has chosen a number. Try to guess it.” << endl;
cin >> AGuess;
int NumberOfGuesses = 1;
while (AGuess != ARandomNumber)
{
if (AGuess < ARandomNumber)
{
cout << "Your guess of " << AGuess << " is too low. Try again" << endl;
cin >> AGuess;
} else
if (AGuess > ARandomNumber)
{
cout << "Your guess of " << AGuess << " is too high. Try again" << endl;
cin >> AGuess;
} else
{
break; // game is over!
}
NumberOfGuesses++;
}
cout << "Congratulations, the number is "<< ARandomNumber<< ". It took " << NumberOfGuesses << " to get it correct." << endl;
return 0;
}