/***************************** Compiler Directives ****************************/
ID: 3643588 • Letter: #
Question
/***************************** Compiler Directives ****************************/
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <string>
#include <ctime>
using namespace std;
/***************************** Function Prototypes ****************************/
//Starts a new game
void NewGame(); //Zero is used to quit the game.
//Displays an old computer name
void computer();
int main()
{
//Local variables
char sentinel; //Replay control variable
/******************* Begin main Function Executables **********************/
//Prompt user with game details
cout << "@
cout << "^ * * * v" << endl;
cout << "^ * * *** v" << endl;
cout << "^ * * ***** v" << endl;
cout << "^ ********* ******* v" << endl;
cout << "^ * * * v" << endl;
cout << "^ * * \ / * v" << endl;
cout << "^ * * | | * v" << endl;
cout << "^ * __ * v" << endl;
cout << "^ * * v" << endl;
cout << "^ * * v" << endl;
cout << "^ ******* * v" << endl;
cout << "^ ***** * v" << endl;
cout << "^ *** * v" << endl;
cout << "^ * ********* v" << endl;
cout << "@
cout << " Welcome to the High-Low game! " << endl;
cout << "In this game you will attempt to " << endl;
cout << "guess a computer generated number" << endl;
cout << " (1 to 100)! Good Luck! " << endl;
//Start game
do
{
//New game
NewGame();
//Replay?
cout << "Computer: Would you like to play again?(Enter Y or N): ";
cin >> sentinel;
}
while (sentinel != 'N');
//Hold execution on screen
system("pause");
//Indicate to OS successful termination of program
return 0;
} //End main
//Starts a new game
void NewGame()
{
//Local Constants
const int cheatcode = 1969; //Shows the user the correct answer while playing
//Local Variables
int guesses[10]; //The user's recorded guesses
int guess; //The user's guess
int print; //The displayed guess
int pass; //The current attempt
int newNumber; //The randomly generated number
char sentinel; //The control to quit the game
/******************* Begin NewGame Function Executables *******************/
//Initiates the srand for time dependent randomizations
srand(static_cast<unsigned int>(time(0)));
//Generates new number
newNumber = (rand() % abs(100)) + 1;
//Wipe guesses array
for (pass = 0; pass < 10; pass++)
{
guesses[pass] = 0;
}
//Prompt user to enter first guess
cout << "I'm thinking of a number..." << endl;
cout << "You: Is your number... ";
cin >> guess;
//Game loop
for (pass = 0; pass <= 9; pass++)
{
if (guess == cheatcode || (guess <= 100 && guess >= 1))
{
//Cheat Code
if (guess == cheatcode)
{
//Displays cheat code message
cout << "Computer: I'm thinking of " << newNumber
<< ", so don't even think about using it!"<< endl
<< "Computer: Zing... Living like it's 1969 is so low of you. What?!?! It's..." << endl;
}
//Guess checker
if (guess > newNumber)
{
//Update guess
cout << "Computer: Too high! " <<
10 - (pass + 1) << " attempts left human." << endl;
//Record guess
guesses[pass] = guess;
//Next guess
cout << "You: Is your number... ";
cin >> guess;
}
else if (guess < newNumber)
{
//Update guess
cout << "Computer: Too low! " <<
10 - (pass + 1) << " attempts left human." << endl;
//Record guess
guesses[pass] = guess;
//Next guess
cout << "You: Is your number... ";
cin >> guess;
}
if (pass == 9)
{
pass++;
//Record guess
guesses[pass] = guess;
//Exhoasted attempts message
cout << "Computer: You've used all your attempts human!"
<< endl << "A ";
computer();
cout << " is better than you!" << endl
<< "I was thinking of " << newNumber << ". Heres your guesses:" << endl;
}
else if (guess == newNumber)
{
pass++;
//Record guess
guesses[pass] = guess;
//Clear Screen
system ("cls");
//Prompt user with congratulatory message
cout << "Computer: Congratulations Human!!! It only took you " << (pass + 1);
if (pass == 0) {cout << " guess!";}
else {cout << " guesses!";}
cout << endl << "Reminds me of that slow ";
computer();
cout << " I used to play with..." << endl
<< "Heres your guesses:" << endl;
}
//Display guesses
if (pass == 9 || guess == newNumber)
{
for (print = 0; print <= pass; print++)
{
cout << "Guess " << print + 1 << ": " << guesses[print] << endl;
}
pass = 10;
}
}
else //Invalid guess
{
cout << "Computer: Invalid answer human! " << 10 - (pass + 1) << " attempts left human." << endl;
pass--;
//Next guess
cout << "You: Is your number... ";
cin >> guess;
}
}
} //End NewGame
//Displays an old computer name
void computer()
{
//Initiates the srand for time dependent randomizations
srand(static_cast<unsigned int>(time(0)));
//Local Variables
string computer[10];
/******************* Begin computer Function Executables ******************/
//Old computer names
computer[0] = "1975 MITS Altair 8800";
computer[1] = "1976 Hewlett-Packard 9825";
computer[2] = "1977 Radio Shack TRS-80";
computer[3] = "1978 VideoBrain Family Computer";
computer[4] = "1980 Radio Shack Pocket Computer";
computer[5] = "1982 Kaypro II";
computer[6] = "1983 Tomy Tutor";
computer[7] = "1988 Apple IIc Plus";
computer[8] = "1989 Atari Stacy";
computer[9] = "1973 Wang 2200";
//Displays an old computer name
cout << computer[((rand() % abs(10)) + 1)];
} //End computer
Explanation / Answer
There was a problem with your invalid guess section as well... I have corrected it. The game is working fine now. Check it. /***************************** Compiler Directives ****************************/ #include <cstdlib> #include <iostream> #include <cmath> #include <string> #include <ctime> using namespace std; /***************************** Function Prototypes ****************************/ //Starts a new game void NewGame(); //Zero is used to quit the game. //Displays an old computer name void computer(); int main() { //Local variables char sentinel; //Replay control variable /******************* Begin main Function Executables **********************/ //Prompt user with game details cout << "@<![CDATA[$COMPUTER]]>@" << endl; cout << "^ * * * v" << endl; cout << "^ * * *** v" << endl; cout << "^ * * ***** v" << endl; cout << "^ ********* ******* v" << endl; cout << "^ * * * v" << endl; cout << "^ * * \ / * v" << endl; cout << "^ * * | | * v" << endl; cout << "^ * __ * v" << endl; cout << "^ * * v" << endl; cout << "^ * * v" << endl; cout << "^ ******* * v" << endl; cout << "^ ***** * v" << endl; cout << "^ *** * v" << endl; cout << "^ * ********* v" << endl; cout << "@$HUMAN<![CDATA[$HUMAN]]>$$$@" << endl; cout << " Welcome to the High-Low game! " << endl; cout << "In this game you will attempt to " << endl; cout << "guess a computer generated number" << endl; cout << " (1 to 100)! Good Luck! " << endl; //Start game do { //New game NewGame(); //Replay? cout << "Computer: Would you like to play again?(Enter Y or N): "; cin >> sentinel; } while (sentinel != 'N'); //Hold execution on screen system("pause"); //Indicate to OS successful termination of program return 0; } //End main //Starts a new game void NewGame() { //Local Constants const int cheatcode = 1969; //Shows the user the correct answer while playing //Local Variables int guesses[10]; //The user's recorded guesses int guess; //The user's guess int print; //The displayed guess int pass; //The current attempt int newNumber; //The randomly generated number char sentinel; //The control to quit the game /******************* Begin NewGame Function Executables *******************/ //Initiates the srand for time dependent randomizations srand(static_cast<unsigned int>(time(0))); //Generates new number newNumber = (rand() % abs(100)) + 1; //Wipe guesses array for (pass = 0; pass < 10; pass++) { guesses[pass] = 0; } //Prompt user to enter first guess cout << "I'm thinking of a number..." << endl; cout << "You: Is your number... "; cin >> guess; //Game loop for (pass = 0; pass < 9; pass++) { if (guess == cheatcode || (guess <= 100 && guess >= 1)) { //Cheat Code if (guess == cheatcode) { //Displays cheat code message cout << "Computer: I'm thinking of " << newNumber << ", so don't even think about using it!"<< endl << "Computer: Zing... Living like it's 1969 is so low of you. What?!?! It's..." << endl; } //Guess checker if (guess > newNumber) { //Update guess cout << "Computer: Too high! " << 10 - (pass + 1) << " attempts left human." << endl; //Record guess guesses[pass] = guess; //Next guess cout << "You: Is your number... "; cin >> guess; } else if (guess < newNumber) { //Update guess cout << "Computer: Too low! " << 10 - (pass + 1) << " attempts left human." << endl; //Record guess guesses[pass] = guess; //Next guess cout << "You: Is your number... "; cin >> guess; } if (pass == 8) { pass++; //Record guess guesses[pass] = guess; //Exhoasted attempts message cout << "Computer: You've used all your attempts human!" << endl << "A "; computer(); cout << " is better than you!" << endl << "I was thinking of " << newNumber << ". Heres your guesses:" << endl; } else if (guess == newNumber) { pass++; //Record guess guesses[pass] = guess; //Clear Screen system ("cls"); //Prompt user with congratulatory message cout << "Computer: Congratulations Human!!! It only took you " << (pass + 1); if (pass == 0) {cout << " guess!";} else {cout << " guesses!";} cout << endl << "Reminds me of that slow "; computer(); cout << " I used to play with..." << endl << "Heres your guesses:" << endl; } //Display guesses if (pass == 9 || guess == newNumber) { for (print = 0; print <= pass; print++) { cout << "Guess " << print + 1 << ": " << guesses[print] << endl; } pass = 10; } } else //Invalid guess { cout << "Computer: Invalid answer human! " << 10 - (pass + 1) << " attempts left human." << endl; //Next guess cout << "You: Is your number... "; cin >> guess; } } } //End NewGame //Displays an old computer name void computer() { //Initiates the srand for time dependent randomizations srand(static_cast<unsigned int>(time(0))); //Local Variables string computer[10]; /******************* Begin computer Function Executables ******************/ //Old computer names computer[0] = "1975 MITS Altair 8800"; computer[1] = "1976 Hewlett-Packard 9825"; computer[2] = "1977 Radio Shack TRS-80"; computer[3] = "1978 VideoBrain Family Computer"; computer[4] = "1980 Radio Shack Pocket Computer"; computer[5] = "1982 Kaypro II"; computer[6] = "1983 Tomy Tutor"; computer[7] = "1988 Apple IIc Plus"; computer[8] = "1989 Atari Stacy"; computer[9] = "1973 Wang 2200"; //Displays an old computer name cout << computer[((rand() % abs(10)) + 1)]; } //End computerRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.