Homework 4 Solution


Category: Tag:


Instructions: Create a subdirectory named “hw4” in your cs410 directory. Use that subdirectory for your all file submissions on this assignment. At the end of the homework assignment, these two files should be found in your hw4 directory:

  1. a single C++ compilable file containing a program written in C++ named “hw4.cpp”

  1. a “typescript” file demonstating program compilation, execution and testing. Use the commands below at the UNIX prompt to generate the typescript file:

script command to start a typescript.

ls -l to list files in your directory and write date/time cat hw4.cpp to print out solution file

g++ -o hw4 hw4.cpp to compile program ./hw4 to execute program

exit command to end typescript file

Background: In this assignment, you are going to write a menu driven program to help Hans out with his “health management issues”. Yep, indeed, it’s no longer enough to just take care of yourself by eating right and getting enough exercise. You had better spend a lot of money, have a personal health trainer, a membership to a gym, take way too many pills and subscribe to every hair-brained self-help scheme alive. But I digress. This program will present the user with several options concerned with improving health in varied manners. The user will choose an option, following instructions about prompted information, receive the intended recommendation, and then have the menu presented to them once again. Of course, the user can quit at any point. Your code will be implemented using functions. Now you will have your practice with them.

Specifications: You are to use functions in the program. In fact, below is a description of the functions we want you to use. Your program will present a greeting and menu of options to the user. It should look something like this:

Health-o-Matic Options


    1. Taking a Walk!

    2. Your Medications

    3. Nap Time!!

    4. Caloric Intake

    5. Quit

  1. When the user chooses option 1, the program should prompt for and read in the number of “legs” of the walk the user wishes to have. The total number of steps from the “legs” should be computed and displayed.

  1. Option 2 chosen will trigger prompts for the user to input their current anxiety level (1 -> 10) and the day of the week (1 -> 7). From this information, the program will compute and output the number of pills the user should take that day.

  1. If option 3 is chosen, your program is to refuse to do anything if both options 1 and 2 haven’t yet been chosen during that run of the program. That is because the computations for option 3 require the information from options 1 and 2. Now, if options 1 and 2 have indeed been chosen, then your program code for option 3 starts by prompting the user for the number of hours they slept the previous night. This

information, along with the computed values from options 1 and 2, will be used to compute and output the number of minutes you may nap for.

  1. Choosing option 4 will initiate prompts for body weight, body height, and room temperature. From this, the code should compute and output caloric intake allowable at that moment.

  1. Choosing quit will terminate the program….of course.

Details: First, you are required to use the switch statement for handling the options chosen from the menu. Secondly, you will use functions for the program and they are laid out here:

1. A function that does nothing more than displaying the greeting for the program. It should have no parameters and return nothing.

2. A function to display the menu and reads in a response (character) from the user and returns it to the main function. It should NOT “input cleanse”; any response from the user should be handled by the switch in main.

3. A function that will prompt for and read in a positive integer for the number of legs of the walk. This (cleansed) value is returned to the calling function (presumably main).

4. A function to which you pass the value returned by the preceding function, and it will output the total number of steps according to:









e.g. if number of legs is 4, then total number of steps = 4! = 4*3*2*1=24 steps.


A function that prompts for and reads in anxiety level (valid input is 1 -> 10) and returns it.


A function that prompts for and reads in the day (valid input is 1 -> 7) and returns it.


A function to which you pass the two values above (anxiety and day) and it returns the number of pills

the user should take according to:

number of pillsanxiety day , if this is non negative; 0 otherwise.


A function to which you pass the number of pills and it will output it to the screen.


A function that prompts for and reads in the number of hours slept the previous night.


A function to which you pass the number of hours slept, the distance walked (i.e. total number of steps)

as determined by option 1, and the number of pills as determined by option 2. It will return the number

of minutes for a nap according to:

minuteshrs slept

dist walked

number of pills

if 0, divide by 1

11. A function to output the number of minutes for a nap. It will have one parameter.

12. A function which prompts for and reads in the user’s weight, height, and room temp (along with the distance walked), then computes and returns calories according to the formula


6 wt2 ht


dist . walked


13. A function to which you pass the value from #12 above and it will output it.

For the foregoing functions, if they are to prompt and input values, they should “cleanse input” with reasonable limits. For example, prompting for a weight should reject responses that are non-positive and those over 800lbs.

When you submit: As usual, when you submit, you are all to enter the same information so as not to drive the graders crazy. Following these steps:

choose option 3 (better get an error message!) choose option 4 (get an error message)

choose option 1 and enter 6 (6 legs for the walk) choose option 3 (again, error!)

choose option 2 and enter anxiety of 3 and day 5 choose option 3 and enter 5 hrs sleep

choose option 4 and enter 250 lbs, 72 inches, and 79.7 F. quit

And, as always, let your TAs or instructor know if you need any help.

error: Content is protected !!