Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

Implement a game of hi-lo. First, your program should pick a random integer between 1 and 100. The user is given 7 tries to guess the number.

If the user does not guess the correct number, the program should tell them whether they guessed too high or too low. If the user guesses the right number, the program should tell them they won. If they run out of guesses, the program should tell them they lost, and what the correct number is. At the end of the game, the user should be asked if they want to play again. If the user doesn’t enter ‘y’ or ‘n’, ask them again.

Note: You do not need to implement error handling for the user’s guess.

Implement a game of hi-lo. First, your program should pick a random integer between 1 and 100. The user is given 7 tries to guess the number.

If the user does not guess the correct number, the program should tell them whether they guessed too high or too low. If the user guesses the right number, the program should tell them they won. If they run out of guesses, the program should tell them they lost, and what the correct number is. At the end of the game, the user should be asked if they want to play again. If the user doesn’t enter ‘y’ or ‘n’, ask them again.

Note: You do not need to implement error handling for the user’s guess.

Implement a game of hi-lo. First, your program should pick a random integer between 1 and 100. The user is given 7 tries to guess the number.

If the user does not guess the correct number, the program should tell them whether they guessed too high or too low. If the user guesses the right number, the program should tell them they won. If they run out of guesses, the program should tell them they lost, and what the correct number is. At the end of the game, the user should be asked if they want to play again. If the user doesn’t enter ‘y’ or ‘n’, ask them again.

Note: You do not need to implement error handling for the user’s guess.

Rollback to Revision 1
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419
#include <ios> // `std::streamsize`
#include <iostream> // `std::cin`, `std::cout`
#include <limits> // `std::numeric_limits`
#include <cstdlib> // `rand()`, `srand()`
#include <ctime> // `time()`
/* Get a random int in the range [min, max] (max inclusive). */
// CSRE: Take note: not my code, so don't review this function, please :)
int getRandomInt(int min, int max) {
 // source:
 // http://www.learncpp.com/cpp-tutorial/59-random-number-generation/
 static const double fraction = 1.0 / (RAND_MAX + 1.0);
 return min + static_cast<int>((max - min + 1) * (rand() * fraction));
}
/* Get the user's next guess. `try_` is the try count. */
int getUserGuess(int try_) {
 int guess;
 while (true) {
 std::cout << "Guess # " << try_ << ": ";
 std::cin >> guess;
 if (std::cin.fail()) {
 // extraction failure
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 } else {
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 return guess;
 }
 }
}
/* Play the number guessing game. Return WON_GAME if the user wins the
game, LOST_GAME otherwise. `min` and `max` are the lower and upper
boundbounds for the randomly generated number, respectively. `tries` is the
amount of tries the user gets before having lost the game. */
void playNumberGuessingGame(int min, int max, int tries) {
 std::cout << "Let's play a game. I'm thinking of a number. ";
 std::cout << "You have " << tries << " tries to guess what it is.\n";
 int number = getRandomInt(min, max);
 for (int try_ = 0; try_ < tries; try_++) {
 int guess = getUserGuess(try_);
 if (guess == number) {
 std::cout << "Correct! You win!\n";
 return;
 } else if (guess < number) {
 std::cout << "Your guess is too low.\n";
 } else {
 std::cout << "Your guess is too high.\n";
 }
 }
 // if we fall through, the user has lost :(
 std::cout << "Sorry, you lose. The correct number was " << number << '\n';
}
/* Ask the user if they want to play again. */
bool playAgain() {
 char response;
 while (true) {
 std::cout << "Would you like to play again (y/n)? ";
 std::cin >> response;
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 if (response == 'y') {
 return true;
 } else if (response == 'n') {
 return false;
 }
 }
}
int main() {
 constexpr int MIN = 0;
 constexpr int MAX = 99;
 constexpr int TRIES = 7;
 srand(time(0));
 rand();
 do {
 playNumberGuessingGame(MIN, MAX, TRIES);
 } while (playAgain());
 std::cout << "Thank you for playing.\n";
}
#include <ios> // `std::streamsize`
#include <iostream> // `std::cin`, `std::cout`
#include <limits> // `std::numeric_limits`
#include <cstdlib> // `rand()`, `srand()`
#include <ctime> // `time()`
/* Get a random int in the range [min, max] (max inclusive). */
// CSRE: Take note: not my code, so don't review this function, please :)
int getRandomInt(int min, int max) {
 // source:
 // http://www.learncpp.com/cpp-tutorial/59-random-number-generation/
 static const double fraction = 1.0 / (RAND_MAX + 1.0);
 return min + static_cast<int>((max - min + 1) * (rand() * fraction));
}
/* Get the user's next guess. `try_` is the try count. */
int getUserGuess(int try_) {
 int guess;
 while (true) {
 std::cout << "Guess # " << try_ << ": ";
 std::cin >> guess;
 if (std::cin.fail()) {
 // extraction failure
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 } else {
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 return guess;
 }
 }
}
/* Play the number guessing game. `min` and `max` are the lower and upper
bound for the randomly generated number, respectively. `tries` is the
amount of tries the user gets before having lost the game. */
void playNumberGuessingGame(int min, int max, int tries) {
 std::cout << "Let's play a game. I'm thinking of a number. ";
 std::cout << "You have " << tries << " tries to guess what it is.\n";
 int number = getRandomInt(min, max);
 for (int try_ = 0; try_ < tries; try_++) {
 int guess = getUserGuess(try_);
 if (guess == number) {
 std::cout << "Correct! You win!\n";
 return;
 } else if (guess < number) {
 std::cout << "Your guess is too low.\n";
 } else {
 std::cout << "Your guess is too high.\n";
 }
 }
 // if we fall through, the user has lost :(
 std::cout << "Sorry, you lose. The correct number was " << number << '\n';
}
/* Ask the user if they want to play again. */
bool playAgain() {
 char response;
 while (true) {
 std::cout << "Would you like to play again (y/n)? ";
 std::cin >> response;
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 if (response == 'y') {
 return true;
 } else if (response == 'n') {
 return false;
 }
 }
}
int main() {
 constexpr int MIN = 0;
 constexpr int MAX = 99;
 constexpr int TRIES = 7;
 srand(time(0));
 rand();
 do {
 playNumberGuessingGame(MIN, MAX, TRIES);
 } while (playAgain());
 std::cout << "Thank you for playing.\n";
}
#include <ios> // `std::streamsize`
#include <iostream> // `std::cin`, `std::cout`
#include <limits> // `std::numeric_limits`
#include <cstdlib> // `rand()`, `srand()`
#include <ctime> // `time()`
/* Get a random int in the range [min, max] (max inclusive). */
// CSRE: Take note: not my code, so don't review this function, please :)
int getRandomInt(int min, int max) {
 // source:
 // http://www.learncpp.com/cpp-tutorial/59-random-number-generation/
 static const double fraction = 1.0 / (RAND_MAX + 1.0);
 return min + static_cast<int>((max - min + 1) * (rand() * fraction));
}
/* Get the user's next guess. `try_` is the try count. */
int getUserGuess(int try_) {
 int guess;
 while (true) {
 std::cout << "Guess # " << try_ << ": ";
 std::cin >> guess;
 if (std::cin.fail()) {
 // extraction failure
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 } else {
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 return guess;
 }
 }
}
/* Play the number guessing game. Return WON_GAME if the user wins the
game, LOST_GAME otherwise. `min` and `max` are the lower and upper
bounds for the randomly generated number, respectively. `tries` is the
amount of tries the user gets before having lost the game. */
void playNumberGuessingGame(int min, int max, int tries) {
 std::cout << "Let's play a game. I'm thinking of a number. ";
 std::cout << "You have " << tries << " tries to guess what it is.\n";
 int number = getRandomInt(min, max);
 for (int try_ = 0; try_ < tries; try_++) {
 int guess = getUserGuess(try_);
 if (guess == number) {
 std::cout << "Correct! You win!\n";
 return;
 } else if (guess < number) {
 std::cout << "Your guess is too low.\n";
 } else {
 std::cout << "Your guess is too high.\n";
 }
 }
 // if we fall through, the user has lost :(
 std::cout << "Sorry, you lose. The correct number was " << number << '\n';
}
/* Ask the user if they want to play again. */
bool playAgain() {
 char response;
 while (true) {
 std::cout << "Would you like to play again (y/n)? ";
 std::cin >> response;
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 if (response == 'y') {
 return true;
 } else if (response == 'n') {
 return false;
 }
 }
}
int main() {
 constexpr int MIN = 0;
 constexpr int MAX = 99;
 constexpr int TRIES = 7;
 srand(time(0));
 rand();
 do {
 playNumberGuessingGame(MIN, MAX, TRIES);
 } while (playAgain());
 std::cout << "Thank you for playing.\n";
}
deleted 69 characters in body
Source Link
Daniel
  • 4.6k
  • 2
  • 18
  • 40
#include <ios> // `std::streamsize`
#include <iostream> // `std::cin`, `std::cout`
#include <limits> // `std::numeric_limits`
#include <cstdlib> // `rand()`, `srand()`
#include <ctime> // `time()`
/* Get a random int in the range [min, max] (max inclusive). */
// CSRE: Take note: not my code, so don't review this function, please :)
int getRandomInt(int min, int max) {
 // source:
 // http://www.learncpp.com/cpp-tutorial/59-random-number-generation/
 static const double fraction = 1.0 / (RAND_MAX + 1.0);
 return min + static_cast<int>((max - min + 1) * (rand() * fraction));
}
/* Get the user's next guess. `try_` is the try count. */
int getUserGuess(int try_) {
 int guess;
 while (true) {
 std::cout << "Guess # " << try_ << ": ";
 std::cin >> guess;
 if (std::cin.fail()) {
 // extraction failure
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 } else {
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 return guess;
 }
 }
}
/* Play the number guessing game. Return WON_GAME if the user wins the
game, LOST_GAME otherwise. `min` and `max` are the lower and upper
boundsbound for the randomly generated number, respectively. `tries` is the
amount of tries the user gets before having lost the game. */
void playNumberGuessingGame(int min, int max, int tries) {
 std::cout << "Let's play a game. I'm thinking of a number. ";
 std::cout << "You have " << tries << " tries to guess what it is.\n";
 int number = getRandomInt(min, max);
 for (int try_ = 0; try_ < tries; try_++) {
 int guess = getUserGuess(try_);
 if (guess == number) {
 std::cout << "Correct! You win!\n";
 return;
 } else if (guess < number) {
 std::cout << "Your guess is too low.\n";
 } else {
 std::cout << "Your guess is too high.\n";
 }
 }
 // if we fall through, the user has lost :(
 std::cout << "Sorry, you lose. The correct number was " << number << '\n';
}
/* Ask the user if they want to play again. */
bool playAgain() {
 char response;
 while (true) {
 std::cout << "Would you like to play again (y/n)? ";
 std::cin >> response;
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 if (response == 'y') {
 return true;
 } else if (response == 'n') {
 return false;
 }
 }
}
int main() {
 constexpr int MIN = 0;
 constexpr int MAX = 99;
 constexpr int TRIES = 7;
 srand(time(0));
 rand();
 do {
 playNumberGuessingGame(MIN, MAX, TRIES);
 } while (playAgain());
 std::cout << "Thank you for playing.\n";
}
#include <ios> // `std::streamsize`
#include <iostream> // `std::cin`, `std::cout`
#include <limits> // `std::numeric_limits`
#include <cstdlib> // `rand()`, `srand()`
#include <ctime> // `time()`
/* Get a random int in the range [min, max] (max inclusive). */
// CSRE: Take note: not my code, so don't review this function, please :)
int getRandomInt(int min, int max) {
 // source:
 // http://www.learncpp.com/cpp-tutorial/59-random-number-generation/
 static const double fraction = 1.0 / (RAND_MAX + 1.0);
 return min + static_cast<int>((max - min + 1) * (rand() * fraction));
}
/* Get the user's next guess. `try_` is the try count. */
int getUserGuess(int try_) {
 int guess;
 while (true) {
 std::cout << "Guess # " << try_ << ": ";
 std::cin >> guess;
 if (std::cin.fail()) {
 // extraction failure
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 } else {
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 return guess;
 }
 }
}
/* Play the number guessing game. Return WON_GAME if the user wins the
game, LOST_GAME otherwise. `min` and `max` are the lower and upper
bounds for the randomly generated number, respectively. `tries` is the
amount of tries the user gets before having lost the game. */
void playNumberGuessingGame(int min, int max, int tries) {
 std::cout << "Let's play a game. I'm thinking of a number. ";
 std::cout << "You have " << tries << " tries to guess what it is.\n";
 int number = getRandomInt(min, max);
 for (int try_ = 0; try_ < tries; try_++) {
 int guess = getUserGuess(try_);
 if (guess == number) {
 std::cout << "Correct! You win!\n";
 return;
 } else if (guess < number) {
 std::cout << "Your guess is too low.\n";
 } else {
 std::cout << "Your guess is too high.\n";
 }
 }
 // if we fall through, the user has lost :(
 std::cout << "Sorry, you lose. The correct number was " << number << '\n';
}
/* Ask the user if they want to play again. */
bool playAgain() {
 char response;
 while (true) {
 std::cout << "Would you like to play again (y/n)? ";
 std::cin >> response;
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 if (response == 'y') {
 return true;
 } else if (response == 'n') {
 return false;
 }
 }
}
int main() {
 constexpr int MIN = 0;
 constexpr int MAX = 99;
 constexpr int TRIES = 7;
 srand(time(0));
 rand();
 do {
 playNumberGuessingGame(MIN, MAX, TRIES);
 } while (playAgain());
 std::cout << "Thank you for playing.\n";
}
#include <ios> // `std::streamsize`
#include <iostream> // `std::cin`, `std::cout`
#include <limits> // `std::numeric_limits`
#include <cstdlib> // `rand()`, `srand()`
#include <ctime> // `time()`
/* Get a random int in the range [min, max] (max inclusive). */
// CSRE: Take note: not my code, so don't review this function, please :)
int getRandomInt(int min, int max) {
 // source:
 // http://www.learncpp.com/cpp-tutorial/59-random-number-generation/
 static const double fraction = 1.0 / (RAND_MAX + 1.0);
 return min + static_cast<int>((max - min + 1) * (rand() * fraction));
}
/* Get the user's next guess. `try_` is the try count. */
int getUserGuess(int try_) {
 int guess;
 while (true) {
 std::cout << "Guess # " << try_ << ": ";
 std::cin >> guess;
 if (std::cin.fail()) {
 // extraction failure
 std::cin.clear();
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 } else {
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 return guess;
 }
 }
}
/* Play the number guessing game. `min` and `max` are the lower and upper
bound for the randomly generated number, respectively. `tries` is the
amount of tries the user gets before having lost the game. */
void playNumberGuessingGame(int min, int max, int tries) {
 std::cout << "Let's play a game. I'm thinking of a number. ";
 std::cout << "You have " << tries << " tries to guess what it is.\n";
 int number = getRandomInt(min, max);
 for (int try_ = 0; try_ < tries; try_++) {
 int guess = getUserGuess(try_);
 if (guess == number) {
 std::cout << "Correct! You win!\n";
 return;
 } else if (guess < number) {
 std::cout << "Your guess is too low.\n";
 } else {
 std::cout << "Your guess is too high.\n";
 }
 }
 // if we fall through, the user has lost :(
 std::cout << "Sorry, you lose. The correct number was " << number << '\n';
}
/* Ask the user if they want to play again. */
bool playAgain() {
 char response;
 while (true) {
 std::cout << "Would you like to play again (y/n)? ";
 std::cin >> response;
 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 if (response == 'y') {
 return true;
 } else if (response == 'n') {
 return false;
 }
 }
}
int main() {
 constexpr int MIN = 0;
 constexpr int MAX = 99;
 constexpr int TRIES = 7;
 srand(time(0));
 rand();
 do {
 playNumberGuessingGame(MIN, MAX, TRIES);
 } while (playAgain());
 std::cout << "Thank you for playing.\n";
}
Source Link
Daniel
  • 4.6k
  • 2
  • 18
  • 40
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /