1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main ()
{
string playerOneName, playerTwoName;
int numberOne, numberTwo;
int hiddenNumber;
int playerOneGuess, playerTwoGuess;
int guessAgain;
cout << "Welcome to the guessing game! \nIn this game for two, the first player to guess the hidden number wins." << endl;
cout << "Player one, please enter your first name." << endl;
cin >> playerOneName;
cout << "Player two, please enter your first name." << endl;
cin >> playerTwoName;
cout << "Hello " << playerOneName << " and " << playerTwoName;
cout << ". You both will now need to choose a private number. \nPlease select between 0 and 100." << endl;
cout << playerOneName << ", please enter your private number." << endl;
cin >> numberOne;
cout << string (50, '\n');
cout << playerTwoName << ", please enter your private number." << endl;
cin >> numberTwo;
cout << string (50, '\n');
hiddenNumber = abs(numberTwo - numberOne);
cout << "Now that you have both entered your private numbers, it's time to guess!" << endl;
guessAgain = 1;
while (guessAgain = 1)
{
cout << playerOneName << ", please enter your guess between 0 and 100." << endl;
cin >> playerOneGuess;
if (playerOneGuess == hiddenNumber)
{
cout << "Congratulations, you win! You're much better than " << playerTwoName << "!" << endl;
guessAgain = 0;
}
else if (playerOneGuess < hiddenNumber)
{
cout << "Sorry, your guess of " << playerOneGuess << " was too low." << endl;
guessAgain = 1;
}
else cout << "Sorry, your guess of " << playerOneGuess << " was too high." << endl;
guessAgain = 1;
if (guessAgain == 1)
{
cout << playerTwoName << ", please enter your guess between 0 and 100." << endl;
cin >> playerTwoGuess;
if (playerTwoGuess == hiddenNumber)
{
cout << "Congratulations, you win! You're much better than " << playerOneName << "!" << endl;
guessAgain = 0;
}
else if (playerTwoGuess < hiddenNumber)
{
cout << "Sorry, your guess of " << playerTwoGuess << " was too low." << endl;
guessAgain = 1;
}
else cout << "Sorry, your guess of " << playerTwoGuess << " was too high." << endl;
guessAgain = 1;
}
else cout << "Thanks for playing!" << endl;
}
cout << "Thanks for playing!" << endl;
return 0;
}
|