Hello I was wondering if someone would be able to help me write my code. This is
ID: 3640357 • Letter: H
Question
Hello I was wondering if someone would be able to help me write my code.This is the problem...
Write a number guessing game in which the computer selects a random number in the range of 0 to 100, and users get a maximum of 20 attempts to guess it. At the end of each game the user should be told whether they won or lost, and then whether they want to play again. When the user quits, the program should output the total number of wins and losses. To make the game more intersting, the program should vary the wording of the messages that it outputs for winning, losing (too low or too high), and for asking for another game. Create 10 different messages for each of these 3 cases (winning, losing (too low or too high), and for asking for another game) and use random numbers to choose among them.
This is what I have so far...
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int rnd (void)
{
return (rand() % 100 + 1);
}
void main (void)
{
int guess;
int choice;
char input;
bool playagain;
int number;
ofstream outData;
outData.open("outresults.txt");
playagain = false;
while (!playagain)
{
cout << "Welcome to my number guessing game! " << endl;
cout << " User to guess a number generated randomly by the program " << endl;
cout << "Select number '1' for this game" << endl;
cin >> choice;
while (choice != 1)
{
cout << "Please enter '1' only" << endl;
cout << "Enter again: " ;
cin >> choice;
}
if (choice == 1)
{
bool isGuessed;
int num = rnd();
isGuessed = false;
cout << "Welcome to the guessing game" << endl;
while (!isGuessed)
{
cout << "Enter an integer greater or equal to 0 and less than 100: ";
cin >> guess;
while (guess < 1 || guess > 100)
{
cout << "Your number had been out of range, please enter number between 1 to 100 only." << endl;
cout << "Enter again: " ;
cin >> guess;
}
cout << endl;
if (guess == num)
{
cout << "You guessed the correct number" << endl;
cout << "My random number is " << num << endl;
cout << "Thank you for playing my number guessing game." << endl;
isGuessed = true;
}
else if (guess < num)
{
cout << "Your number is smaller.Guess again";
cout << endl;
}
else
{
cout << "Your number is higher. Guess again";
cout << endl;
}
}
}
cout << "Do you want to play again. " << endl;
cout << "Press 1 to play agin or press 2 to stop the game." << endl;
cin >> number;
while (number < 1 || number > 2)
{
cout << "Press only 1 or 2. " << endl;
cout << "Press a number again." << endl;
cin >> number;
}
if (number == 1)
{
cout << "Lets play again. " << endl;
}
else
{
cout << "Thank you for playing" << endl;
playagain = true;
}
}
}
I would really appreciate if someone could help me out!
Explanation / Answer
I leave the winning messages and losing (low & high) messages for you to fill in by yourself.
If you want random number from 1-100, change LEFT_BOUND to 1.
#include <iostream>
#include <time.h>
using namespace std;
//SYMBOLIC CONSTANTS
const int LEFT_BOUND = 0;
const int RIGHT_BOUND = 100;
const int MAX_ATTEMPS = 20;
const int TOTAL_MSG = 10;
const char* WIN_MSG[] = { //WINNING MESSAGES
"Winning message 1",
"Winning message 2",
"Winning message 3",
"Winning message 4",
"Winning message 5",
"Winning message 6",
"Winning message 7",
"Winning message 8",
"Winning message 9",
"Winning message 10"
};
const char* LOSE_H_MSG[] = { //LOSING (too high) MESSAGES
"Losing (too high) message 1",
"Losing (too high) message 2",
"Losing (too high) message 3",
"Losing (too high) message 4",
"Losing (too high) message 5",
"Losing (too high) message 6",
"Losing (too high) message 7",
"Losing (too high) message 8",
"Losing (too high) message 9",
"Losing (too high) message 10"
};
const char* LOSE_L_MSG[] = { //LOSING (too low) MESSAGES
"Losing (too low) message 1",
"Losing (too low) message 2",
"Losing (too low) message 3",
"Losing (too low) message 4",
"Losing (too low) message 5",
"Losing (too low) message 6",
"Losing (too low) message 7",
"Losing (too low) message 8",
"Losing (too low) message 9",
"Losing (too low) message 10"
};
//MAIN
int main()
{
//LOCAL DECLARATIONS
int randomNbr;
char response = 'y';
int guessNbr;
int attemps;
int msgIndex;
int winsCount = 0;
int gamesCount = 0;
//PROCEDURES
srand(time(NULL)); //seed random
//Pint welcome message
cout << "Welcome to my number guessing game! ";
cout << " User is to guess a number ("
<< LEFT_BOUND << " <= n <= " << RIGHT_BOUND << ") ";
//Game procedure
while (toupper(response) == 'Y')
{
//Prepare for a new game
//change LEFT_BOUND to 1 if you want random number from 1-100
randomNbr = rand() % (RIGHT_BOUND - LEFT_BOUND + 1) + LEFT_BOUND;
msgIndex = rand() % TOTAL_MSG;
guessNbr = -1;
attemps = MAX_ATTEMPS;
//Ask for player's guesses and check with generated random number
while (guessNbr != randomNbr && attemps)
{
cout << "Enter your guess: ";
cin >> guessNbr;
attemps--;
if (guessNbr > randomNbr)
{
cout << "Your number is too high. Guess again ";
}
else
{
cout << "Your number is too low. Guess again ";
}
cout << "You have " << attemps << " attemp(s) left ";
}
//Decid and count game result
if (guessNbr == randomNbr)
{
cout << " You win!!! "
<< WIN_MSG[msgIndex] << endl;
winsCount++;
}
else if (guessNbr > randomNbr)
{
cout << "You lose! "
<< LOSE_H_MSG[msgIndex] << endl;
}
else
{
cout << "You lose! "
<< LOSE_L_MSG[msgIndex] << endl;
}
gamesCount++;
//Ask if user wants to play again
cout << " Do you want to play again?(y/n) ";
cin >> response;
cout << endl << endl;
}
//Print to screen some game statistics
cout << "Wins: " << winsCount << endl
<< "Loses: " << gamesCount - winsCount << endl
<< "Total games played: " << gamesCount << endl;
//End
cout << endl;
cin.sync();
cin.get();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.