// --------------------------------------------------------------------------- // BestGuess.cpp // // Written By: YourNameGoesHere // Last Modified: DueDateGoesHere // // Description: // Delete this and // Write something appropriate here that describes what the program does // --------------------------------------------------------------------------- #include // include iostream to be able to use std::cin and std::cout #include // include cstdlib for use of srand() and rand() functions #include // include ctime for use of time() function using namespace std; // make use of the appropriate namespace for cin and cout // --------------------------------------------------------------------------- // Declare global function prototypes to be used by main() function // --------------------------------------------------------------------------- int GetGuess(); int CalcRelation(int guess, int secretNum); // --------------------------------------------------------------------------- // Declare and define global constants // --------------------------------------------------------------------------- const int QUIT_REQUEST = -999; // declare and define QUIT_REQUEST as a const int with value -999 // --------------------------------------------------------------------------- // main() --- it all begins here // --------------------------------------------------------------------------- int main() { // Setup - Seed the random number generator srand(static_cast(time(0))); // Setup - Init secretNumber = some random number from 1 to 100, // and Init other local vars: tries = 0, guess = 0, relation = QUIT_REQUEST int secretNumber = rand() % 100 + 1; int tries = 0; int guess = 0; int relation = QUIT_REQUEST; // Setup - Offer initial welcome screen with directions cout << "Welcome to Best Guess!!!\n\n"; cout << "I have selected a number from 1 to 100.\n"; cout << "Try to guess it.\n\n"; // Create the Game Loop do { // Call a function to ask the user for a number and return it // Store the result in the local variable named: guess guess = GetGuess(); // Increment local variable: tries ++tries; // Call a function // that takes the guess and secretNumber as parameters // that returns -1 for too low, 1 for too high, 0 for match, // and const value: QUIT_REQUEST for quit (i.e. guess <= 0) // Store the return value in local variable named: relation relation = CalcRelation(guess, secretNumber); // Process the value of the variable relation accordingly // Use a switch statement as outlined based on // the value of local variable named: relation switch (relation) { case QUIT_REQUEST: cout << "You want to quit... awwwww but why?\n"; cout << "See you later.\n"; break; case 1: cout << "Too High. Try again\n"; break; case -1: cout << "Too Low. Try again\n"; break; case 0: cout << "\nAmazing! How did you know?\n"; cout << "You got it in " << tries << " guesses.\n"; break; default: cout <<"\nERROR --- relation == " << relation << endl; } // end switch } while ( (relation != 0) && (relation != QUIT_REQUEST) ); return 0; // return 0 to indicate success to the operating system } // end main() // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // Definition of Global Functions follows // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // GetGuess // Asks the user for a number and returns it // Also indicates that a guess of zero will quit out of the game // --------------------------------------------------------------------------- int GetGuess() { int guess; cout << "What number do you guess (0 to quit): "; cin >> guess; return guess; } // --------------------------------------------------------------------------- // CalcRelation // Takes integers: guess and secretNum as input parameters // Returns -1 for too low, 1 for too high, 0 for match, // and const value: QUIT_REQUEST for quit // quit will be triggered if gues is <= 0 // --------------------------------------------------------------------------- int CalcRelation(int guess, int secretNum) { int retVal = QUIT_REQUEST; // default retVal to QUIT_REQUEST if (guess <= 0) { retVal = QUIT_REQUEST; } else if (guess > secretNum) { retVal = 1; // 1 for too high } else if (guess < secretNum) { retVal = -1; // negative 1 for too low } else { retVal = 0; // zero for match } return retVal; }