In: Computer Science
Hi, how can I create a random guessing game in C++ with 2 players. I got some of the code but not working properly.
//Guess my number game
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int seed = time(0);
srand(seed);
int randomNumber = rand() % 100 + 1;
int userInput1, userInput2;
int counter = 0;
cout << "Welcome to Guess my Number" << endl;
do
{
cout << "PLAYER1 enter a
number: ";
cin >> userInput1;
cout << "PLAYER2 enter a
number: ";
cin >> userInput2;
if (userInput1 ==
randomNumber)
cout <<
"Congratulations you WON!" << endl;
else if (userInput1 >
randomNumber)
cout <<
"Winning number is lower." << endl;
else
cout <<
"Winning number is higher." << endl;
counter++;
cout << endl;
} while (userInput1 != randomNumber);
cout << "The winning number was " <<
randomNumber << endl;
cout << "Number of attempts. " << counter
<< endl;
system("pause");
return 0;
}
Taking input from 2 players was creating all the confusion, your code is logically completely right, few tweaks that were present has been updated, there is only one player who guesses the number.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int seed = time(0);
srand(seed);
int randomNumber = rand() % 100 + 1;
int userInput;
int counter = 0;
cout << "Welcome to Guess my Number" << endl;
do
{
cout << "PLAYER enter a number: ";
cin >> userInput;
if (userInput == randomNumber)
cout << "Congratulations you guessed it right, you WON!!"
<< endl;
else if (userInput > randomNumber)
cout << "Winning number is lower." << endl;
else
cout << "Winning number is higher." << endl;
counter++;
cout << endl;
} while (userInput != randomNumber);
cout << "The winning number was " << randomNumber
<< endl;
cout << "Number of attempts = " << counter <<
endl;
return 0;
}
Here is sample test of the guessing game:-

EDIT : Here this is the fully fledged working model of your guessing game, with two players.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int seed = time(0);
srand(seed);
int randomNumber = rand() % 100 + 1;
int userInput1,userInput2;
int counter = 0;
cout << "Welcome to Guess my Number" << endl;
do
{
cout << "PLAYER 1 enter a number: ";
cin >> userInput1;
cout << "PLAYER 2 enter a number: ";
cin >> userInput2;
if (userInput1 == randomNumber)
cout << "Congratulations you guessed it right, player 1
WON!!" << endl;
else if (userInput1 > randomNumber)
cout << "Winning number is lower than player1's number"
<< endl;
else
cout << "Winning number is higher than player1's number"
<< endl;
if (userInput2 == randomNumber)
cout << "Congratulations you guessed it right, player 2
WON!!" << endl;
else if (userInput2 > randomNumber)
cout << "Winning number is lower than player2's number"
<< endl;
else
cout << "Winning number is higher than player2's number"
<< endl;
counter++;
cout << endl;
} while ((userInput1 != randomNumber) &&(userInput2 != randomNumber));
cout << "The winning number was " << randomNumber
<< endl;
cout << "Number of attempts = " << counter <<
endl;
return 0;
}
