Lab14 - Number Guess Game

About this Page

User inputs guesses as to the value of the random number generated by the program. Program determines whether guess is correct. Displays proper message, corresponding to the accuracy of the guess (too high, too low, just right...).

/* Title      - Number Guess Game
 * File name  - Lab14
 * Programmer - 415 Erich Musick
 * IPO        - Input   - User inputs guesses as to the value of the random
 *                        number generated by the program.
 *              Process - Program determines whether guess is correct.
 *              Output  - Displays proper message, corresponding to the accuracy
 *                        of the guess (too high, too low, just right...)    
*/

/*************************** H E A D E R  F I L E S ***************************/
//#include "iostream.h"
#include "waittoclose.h"    // Add to project waittoclose.cpp
#include "apstring.h"       // Add to project apstring.cpp
#include "randgen.h"       // Add to project randgen.cpp
#pragma hdrstop
//#include "apvector.h"     // DO NOT ADD TO PROJECT apvector.cpp
//#include "apmatrix.h"     // DO NOT ADD TO PROJECT apmatrix.cpp

/******************** F U N C T I O N  P R O T O T Y P E S ********************/

/****************************** F U N C T I O N S *****************************/
int main() {
  apstring response;      // variable used to know whether to continue
  int correctAnswer;      // holds correct number (what computer is thinking of)
  int guess;              // holds user's guesses
  int numberOfGuesses;    // number of times user has guessed
  RandGen random;         // var of type RandGen, for determining correct answer

  do {
    // Get a random number
    correctAnswer = random.RandInt(1,100);

    // Display "welcome"/instructions (displayed each time game is played)
    cout << "I am thinking of a number between 1 and 100. Take a shot at ";
    cout << "guessing the number";
    cout << " that I have picked.";
    cout << endl;
    cout << endl;

    numberOfGuesses = 0;
    guess = 0;
    while (guess != correctAnswer && numberOfGuesses < 7) {
      /* As long as the user's guess isn't correct and they haven't guessed more
         than 7 times, allow them to enter another guess */

      cout << "Enter guess ";
      cout << (numberOfGuesses + 1);
      cout << ": ";
      cin >> guess;

      /* If this isn't the last guess and this guess is wrong, tell the user
         whether the guess is higher or lower than the acutal number */
      if (numberOfGuesses < 6) {
        if (correctAnswer > guess) {
          cout << "Guess is too low. Try a higher number.";
          cout << endl;
        }
        else {
          if (correctAnswer < guess) {
            cout << "Guess is too high. Try a lower number.";
            cout << endl;
          }
        }
      }

      cout << endl;

      // Increment number of guesses
      numberOfGuesses = numberOfGuesses + 1;
    }

    /* Tell the user whether or not they guessed the correct number (after 7
       guesses */
    if (correctAnswer != guess) {
      cout << "Go back to math class. You should be able to guess the correct";
      cout << " answer in no more";
      cout << "than 7 tries. The correct answer was: ";
      cout << correctAnswer;
    }
    else {
      cout << "Guess is correct! It took you ";
      cout << numberOfGuesses;
      cout << " guess(es) to get the correct number.";
    }

    cout << endl;
    cout << endl;

    // Ask if user would like to play again
    cout << "Do you want to play again (y/n)? ";
    cin >> response;

    if (response == "y" || response == "Y") {
      cout << endl;
    }
  } while (response == "y" || response == "Y");

  return 0;
}

/************************** S A M P L E  O U T P U T **************************/

/*               LAB14 by 415 ERICH MUSICK   11:18 on Nov 16 2001

I am thinking of a number between 1 and 100. Take a shot at guessing the number
that I have picked.

Enter guess 1: 50
Guess is too high. Try a lower number.

Enter guess 2: 25
Guess is too high. Try a lower number.

Enter guess 3: 12
Guess is too low. Try a higher number.

Enter guess 4: 18
Guess is too high. Try a lower number.

Enter guess 5: 15
Guess is too high. Try a lower number.

Enter guess 6: 14
Guess is too high. Try a lower number.

Enter guess 7: 13

Guess is correct! It took you 7 guess(es) to get the correct number.

Do you want to play again (y/n)? n

'Esc' closes window.*/

/*               LAB14 by 415 ERICH MUSICK   11:18 on Nov 16 2001

I am thinking of a number between 1 and 100. Take a shot at guessing the number
that I have picked.

Enter guess 1: 50

Guess is correct! It took you 1 guess(es) to get the correct number.

Do you want to play again (y/n)? n

'Esc' closes window.*/

/*               LAB14 by 415 ERICH MUSICK   11:18 on Nov 16 2001

I am thinking of a number between 1 and 100. Take a shot at guessing the number
that I have picked.

Enter guess 1: 50
Guess is too low. Try a higher number.

Enter guess 2: 75
Guess is too low. Try a higher number.

Enter guess 3: 87
Guess is too high. Try a lower number.

Enter guess 4: 81
Guess is too high. Try a lower number.

Enter guess 5: 78

Guess is correct! It took you 5 guess(es) to get the correct number.

Do you want to play again (y/n)? y

I am thinking of a number between 1 and 100. Take a shot at guessing the number
that I have picked.

Enter guess 1: 50
Guess is too low. Try a higher number.

Enter guess 2: 75

Guess is correct! It took you 2 guess(es) to get the correct number.

Do you want to play again (y/n)? n

'Esc' closes window.*/

Return to C++ Snippetts