Question

In: Computer Science

Hi, how can I create a random guessing game in C++ with 2 players. I got...

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;
}

Solutions

Expert Solution

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;
}


Related Solutions

Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
1. [100] Create a C program for a number guessing game. Here are the requirements: a....
1. [100] Create a C program for a number guessing game. Here are the requirements: a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max. b. If the user guesses...
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
Write a C++ console application to simulate a guessing game. Generate a random integer between one...
Write a C++ console application to simulate a guessing game. Generate a random integer between one and 100 inclusive. Ask the user to guess the number. If the user’s number is lower than the random number, let the user know. If the number is higher, indicate that to the user. Prompt the user to enter another number. The game will continue until the user can find out what the random number is. Once the number is guessed correctly, display a...
Hi, I am wondering how to create code that will allow (in c) the console to...
Hi, I am wondering how to create code that will allow (in c) the console to read a password from a .txt file, and have the ability to change this password? I have a function that logs certain users in using simple if statements and hardcoded passwords, but i would like the ability to be able to change the password and read it from a .txt so it is editable instead of having it in a function in the code....
C# programming Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the...
C# programming Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the range of 1 to 20. Each random number is associated with a prize money (from 1 to 10000). Use a Dictionary to store the mapping between the random number and the prize money. Ask user for two distinct numbers, a and b, both from 1 to 20; if a and b are not distinct, or out of range, quit the program Lookup the prize...
How would I create a Hangman game that chooses  a random word and the player needs to...
How would I create a Hangman game that chooses  a random word and the player needs to guess it, letter by letter using  JavaScript and jQuery to allow user interaction. The content and the style of the page must be updated based on user’s input.
I am wanting to know how to write guessing game program in java that prompts the...
I am wanting to know how to write guessing game program in java that prompts the use to enter a capital for a each of 50 states, one state at a time. Upon receiving the user input, the program reports whether the answer is correct or incorrect. Assume 50 states and their capitals are stored in a two-dimensional array. Attached is a copy of the two-dimensional array that can be copied into your program. The user's answer is not case...
hi, can you please create me a game that is not too hard but not too...
hi, can you please create me a game that is not too hard but not too easy using pygame in python. can you also explain the codes in each line ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT