CS010 - Introduction to Computer Science I
Lab 5. Menus; Functions/Calculations (cont)

Parts 1 , 2 , 3 and 4 are required exercises for all students, and should be completed in the lab period

Parts 5 and 6 are challenge exercises (worth a small bonus!) to be attempted in whatever remaining time you have.

Grading
Attendance & participation: 2 points
Parts 1, 2, 3 & 4: 2 points each
Parts 5 & 6 (bonus exercises): 0.25 points each

After this lab you should be:
       Familiar with simple menus structures.
       More comfortable with debugging
       More comfortable with function definitions and function calls.


Part 1:

Experiment with a menu structure that simply outputs the user's choice, and terminates when a particular value is entered.

i.e the prompt would look like:

Input a, b, c or d, or q to quit

and the output would be (user enters c):

You entered c
Input a, b, c or d, or q to quit

or (user enters ABC):

Your input was unacceptable. Try again!
Input a, b, c or d, or q to quit

or (user enters q or Q):

Quitter!
(and then terminates)

Use an if-else construct.


Part 2:

Rewrite Part 1 using a switch construct instead of an if-else construct.

The switch syntax is as follows:

       switch (Controlling_expression)
       {
             case Constant_1:
                  Statement_Sequence_1
                  break;
             case Constant_2:
                  Statement_Sequence_2
                  break;
                          .
                          .
                          .
             case Constant_n:
                  Statement_Sequence_n
                  break;
             default:
                  Default_Statement_Sequence
       }

The Controlling_Expression can return a type char, int, or bool (among others). The computer will look at each case Constant starting from Constant_1 and ending at Constant_n until it finds a constant that is equal to the value of the Controlling_Expression. It then executes the statements following this case until it sees a break; statement. At that point, the computer breaks out of the switch statement.

If there are no case Constants equal to the value of the Controlling_Expression then the statements after the default: label are executed.


Part 3:

Run the program through the debugger, tracing the value of one of your variables through the course of the program.


Part 4:

Create a coin value calculation program. It can take input from the user as following (user responses in bold):

How many pennies do you have? 1

How many nickels do you have? 3

How many dimes do you have? 2

How many quarters do you have? 4

Then the program should print out the value

Total value is: XXX

You should use a function that sends the value of the coin and the number of coins and returns the total value of those type of coins. So, after each input you should have a function call.


Part 5:

Modify the program in part 4 to test each input and then output the values of each variable after the input statements.

This time the program should only accept valid positive integers as input.


Part 6:


Modify your program in part 5 to use a menu that lets the user choose the type of coin they will be inputing the number of. It should allow the user to choose a type of coin to input until they choose to quit. At that point the program should output the total value of all the coins they entered.