PLEASE FILL IN THIS CODE AND FOLLOW THE FORMAT //*******************************
ID: 3680242 • Letter: P
Question
PLEASE FILL IN THIS CODE AND FOLLOW THE FORMAT //************************************************************************************************** // FILE: lab07.cpp // // DESCRIPTION: A simple number guessing game. The computer selects a // secret number within a user specified range. The user // then has a specified number of attempts in which to // correctly guess the secret number. the program provides // helpful feedback after each guess. // // This lab project illustrates the following C++ concepts: // 1. Writing functions. Function calls. Pass parameters by-value. Return values. // 2. Writing loops. // 3. Writing if statements, if-else statements, and if-elseif-elseif... statements. // // AUTHORS: <your-name> (<your-email-address) // <your-partner's-name> (<your-partners-email-address>) // // COURSE: CSE100 Principles of Programming with C++, Spring 2015 // // LAB INFO: Lab Number 7; Date/Time: <your-lab-date-and-time>; TA: <your-lab-ta> // // ------------------------------------------------------------------------------------------------- #include <iostream> using namespace std; // be sure to add any #include directives that you may need ? ? ? //-------------------------------------------------------------------------------------------------- // FUNCTION: int getRangeMaximum() // // Prompts the user to enter an integer value between 10 and 1000 (inclusive). // // if the user enters a number that is not valid, // then display an error message and ask again. // // if the user enters a valid choice, // then return that value. // //-------------------------------------------------------------------------------------------------- ? ? ? //-------------------------------------------------------------------------------------------------- // FUNCTION: int getSecretNumber(int min, int max) // // Generates and returns a "random" number between min value and max value (inclusive). // //-------------------------------------------------------------------------------------------------- ? ? ? //-------------------------------------------------------------------------------------------------- // FUNCTION: int getNumberOfAttemptsAllowed() // // Prompts the user to enter the number of attempts that they will have to guess the secret number. // This value must be in the range of 10 to 100 (inclusive) // // if the user enters a number that is not a valid, // then display an error message and ask again. // // if the user enters a valid number, then return that value. // //-------------------------------------------------------------------------------------------------- ? ? ? //-------------------------------------------------------------------------------------------------- // FUNCTION: int getUsersGuess() // // Prompts the user to enter their guess. // Then return that value. // //-------------------------------------------------------------------------------------------------- ??? //-------------------------------------------------------------------------------------------------- // FUNCTION: void giveFeedback(int usersGuess, int secretNumber) // // Gives user feedback based on their guess. // // if the user's guess is the same as the secret number then display "CORRECT!". // if the user's guess is less than the secret number then display "TOO LOW!". // if the user's guess is greater than the secret number then display "TOO HIGH!". // //-------------------------------------------------------------------------------------------------- ? ? ? //-------------------------------------------------------------------------------------------------- // FUNCTION: bool askUserIfTheyWillPlayAgain() // // Asks the user if they would like to play again. // // if they answer "yes" then return true; // if they answer "no" then return false; // if they give an answer other than "yes" or "no" then ask again. // //-------------------------------------------------------------------------------------------------- ? ? ? // this main function is complete and should not require any modifications int main() { int secretNumber = 0, usersGuess = 0; int numberOfAttemptsAllowed = 0, numberOfGuessesUsed = 0; int max = 0; bool playAgain = true; do { max = getRangeMaximum(); secretNumber = getSecretNumber(1, max); numberOfAttemptsAllowed = getNumberOfAttemptsAllowed(); numberOfGuessesUsed = 0; do { usersGuess = getUsersGuess(); numberOfGuessesUsed++; giveFeedback(usersGuess, secretNumber); } while (usersGuess != secretNumber && numberOfGuessesUsed < numberOfAttemptsAllowed); playAgain = askUserIfTheyWillPlayAgain(); } while (playAgain); return 0; }
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <string.h>
using namespace std;
int getRangeMaximum()
{
int number;
// Prompts the user to enter an integer value between 10 and 1000 (inclusive).
cout <<"Enter an Integer between 10 and 1000 (inclusive): ";
cin >> number;
while(number > 1000 || number < 10 )
{
cout <<" Invalid entry ";
cout <<"Enter an Integer between 10 and 1000 (inclusive): ";
cin >> number;
}
return number;
}
int getSecretNumber(int min, int max)
{
srand(time(NULL));
int randNum = rand()%(max-min + 1) + min;
return randNum;
}
// Generates and returns a "random" number between min value and max value (inclusive).
int getNumberOfAttemptsAllowed()
{
int attempts;
// Prompts the user to enter an integer value between 10 and 1000 (inclusive).
cout <<"Enter the number of attempts, must be in the range of 10 to 100 (inclusive): ";
cin >> attempts;
while(attempts > 1000 || attempts < 10 )
{
cout <<" Invalid entry ";
cout <<"Enter the number of attempts, must be in the range of 10 to 100 (inclusive): ";
cin >> attempts;
}
return attempts;
}
int getUsersGuess()
{
int guess;
//
// Prompts the user to enter an integer value between 10 and 1000 (inclusive).
cout <<"Enter your guess between 10 and 1000 (inclusive): ";
cin >> guess;
return guess;
}
void giveFeedback(int usersGuess, int secretNumber)
{
if(usersGuess > secretNumber) cout <<"TOO HIGH! ";
else if(usersGuess < secretNumber) cout <<"TOO LOW! ";
else if(usersGuess == secretNumber) cout <<"CORRECT! ";
}
bool askUserIfTheyWillPlayAgain()
{
string s;
cout<< "Do you want to play again? (yes or no): ";
cin >> s;
while( s != "yes" && s != "no")
{
cout <<" Invalid entry ";
cout<< "Do you want to play again? (yes or no): ";
cin >> s;
}
if(s == "yes") return true;
else return false;
}
// this main function is complete and should not require any modifications
int main()
{
int secretNumber = 0, usersGuess = 0;
int numberOfAttemptsAllowed = 0, numberOfGuessesUsed = 0;
int max = 0;
bool playAgain = true;
do
{
max = getRangeMaximum();
secretNumber = getSecretNumber(1, max);
numberOfAttemptsAllowed = getNumberOfAttemptsAllowed();
numberOfGuessesUsed = 0;
do
{
usersGuess = getUsersGuess();
numberOfGuessesUsed++;
giveFeedback(usersGuess, secretNumber);
} while (usersGuess != secretNumber && numberOfGuessesUsed < numberOfAttemptsAllowed);
playAgain = askUserIfTheyWillPlayAgain();
} while (playAgain);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.