CS302 / Program 2

Sections 15 & 22
Instructor: Colby O'Donnell

Due Date: Friday, Oct 3, 8:30 AM


Math Menu

Program 2 will test your use of loops and conditionals, as well as giving practice checking input for errors.

----------------------------------------------------------------------

Background

The program you will be writing will consist of three parts: a menu with four choices (one of which is exit), and the three main menu options: converting from a number of seconds to days/hours/minutes/seconds notation, printing the sum of a series of numbers, and taking the average of a list of numbers.

Program requirements

Just like last time, you'll be writing this program yourself. The requirements for the four parts are as follows:
  • The menu itself should repeatedly print out the choices and then prompt the user to enter a number or character indicating their choice. One of the four choices should be to exit the program (by quitting the loop). The menu might look something like this:

    Welcome to Math Menu!
    Choices are:
    [C]onvert from seconds to days/hours/minutes/seconds
    [A]verage a list of numbers
    [S]um a series of numbers
    [E]xit the program

    Enter your choice:

  • The Convert option should prompt the user for a non-negative integer number of seconds, and print out what that is in days, hours, minutes, and seconds. For example:

    Enter an integer number of seconds: -1
    That's not an appropriate input.
    Enter an integer number of seconds: 3600
    3600 seconds is 0 days, 1 hour, 0 minutes, and 0 seconds.

    After this the program would redisplay the main menu. Notice how the output correctly prints "1 hour" and not "1 hours", and that it checks for inappropriate input and prompts again until it gets correct input.

  • The Average option should prompt the user for integers until a negative number is entered. Then it should print out the average of that list. It might look like this:

    Enter a list of numbers ending with a negative number: 3 4 5 11 6 -8
    The average of that list was: 5.8
    After this it returns to the main menu. Make sure you check for division by zero! (There is a way to do this with a for loop if you want to be tricky, but you don't have to.)

  • The Sum option should prompt the user for a number and print out the sum of 1+2+3...+that number. For example, if the user entered 7, the program would print out 28. This should also make sure its input is greater than or equal to 1, and prompt again if not.

Hints and suggestions

  • This is a hard program. Get started on it soon.
  • Work on getting the main menu to work first. There are examples in the book. The main menu should be a do loop that prints the menu, then prompts for a choice, then performs the corresponding action. We recommend you use a switch statement to decide which action to take. Each of the choices (except exit) will also use a loop of their own, possibly of a different type. If the user enters an invalid choice, the program should print an error and then continue in the loop to reprint the menu again.
  • Use cin and cout to prompt the user and display information.
  • Use parentheses to clarify your calculation code.
  • If you need to print a fractional number, use doubles as opposed to floats.
  • Use comments!

What to turn in

  • Use the same hand-in procedure as for program 1.
  • Electronically submit one C++ source file, for example, mathmenu.cpp, and also submit the executable, program2.exe just as we did for program 1. For this assignment, the handin directory is the same as the last one, only instead of putting it in the prog1 directory, you should of course put it in the prog2 directory.
  • Remember, I am taking off points if you don't follow the hand-in procedure correctly. If you have questions, ask early.


(Last modified: 09/25/97)