Question: Intro to C++ 2 - Refactor the Guess Number application we did in chapter 4 so that you move the logic into functions named get_random_number(),

Intro to C++

2 - Refactor the Guess Number application we did in chapter 4 so that you move the logic into functions named get_random_number(), get_user_guess() and check_guess(). Use your best judgement to decide what code to move into which function. Again, create a hierarchy outline (see pg. 249) for the program and place it in the comments section at the top of the app. Its not required to break the functions out into header/implementation files, but you can if you like. Your choice.

#include #include #include

using namespace std;

int main() { int upper_limit = 10; cout << "Guess the number! "; cout << "I'm thinking of a number from 1 to " << upper_limit << " ";

// get a random number between 1 and the upper limit srand(time(nullptr)); // seed the rand() function int number = rand() % upper_limit; // number is >= 0 and < upper_limit ++number; // number is >= 1 and <= upper_limit

int count = 1; int guess = 0; while (guess != number) { cout << "Your guess: "; cin >> guess;

if (guess < 1 || guess > upper_limit) { cout << "Invalid guess. Try again. "; } else if (guess < number) { cout << "Too low. "; ++count; } else if (guess > number) { cout << "Too high. "; ++count; } else { cout << "You guessed it in " << count << " tries. "; } } cout << "Bye! "; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!