## Assignment #3 - Practice with Loops & Functions

Due: Monday Feb 12th at 11:59:00pm

### Objective

This assignment will consist of writing different small programs that involve practice utilizing loops and writing and calling functions. The second part of the vassignment will be the only one that will require the use of a function.

Write the following programs, each in a separate file. Filenames should be:
• numbers.cpp
• currency.cpp
(Note that the filenames are all lowercase)

## Exercise 1

Filename: numbers.cpp

Write a program that will allow the user to enter a set of integers (as many as they want), then prints out a summary of information, as follows:

• Prompt the user continually to enter a number using a loop, until the value 0 is entered, which will be the signal to stop entering values. (This is sometimes known as a "sentinel" value).
• Once the sentinel value has been entered, print out the following information:
• The number of positive values that were input
• The number of negative values that were input
• The sum of the input values
• The average (to two decimal places) of the input values
• Your output must match mine exactly. See the sample runs below

### Sample Runs

(user input is underlined, to distinguish it from output)

#### Sample Run 1

```  Enter a Number (0 to stop): 12
Enter a Number (0 to stop): 4
Enter a Number (0 to stop): -1
Enter a Number (0 to stop): -5
Enter a Number (0 to stop): 18
Enter a Number (0 to stop): 0

# of positives = 3
# of negatives = 2
Sum = 28
Average = 5.60
```

#### Sample Run 2

```  Enter a Number (0 to stop): 4
Enter a Number (0 to stop): 8
Enter a Number (0 to stop): 24
Enter a Number (0 to stop): 94
Enter a Number (0 to stop): -1
Enter a Number (0 to stop): 43
Enter a Number (0 to stop): 13
Enter a Number (0 to stop): 0

# of positives = 6
# of negatives = 1
Sum = 185
Average = 26.43
```

#### Sample Run 3

```  Enter a Number (0 to stop): 0

# of positives = 0
# of negatives = 0
Sum = 0
Average = 0.00
```

### Exercise 2

Filename: currency.cpp
1. Write a function called toUSD that takes in two parameters, one double representing an amount of money in a foreign currency and a character representing the foreign currency you're converting from. The function will compute and return the amount of money in US Dollars as a double. Declare three const double variables inside this function to represent POUND EURO and YEN conversion rates (see #5) and use these variables in your calculations. The character parameter taken in by the function will either be P for Pounds, Y for Yen, or E for euros. Allow both uppercase and lowercase P, Y, and E to represent pounds, euros, and yen. This is all this function should do. NO user input or output to the screen should occur in this function.

2. Write a function named menu() that takes in no parameters and returns no value. This function will simply be called anytime the following menu needs to be printed:
```Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
```
Printing the menu is all that this function should do. No more, no less.

3. To test these functions, write a main() routine (in the same file) that first welcomes the user and then enters a loop in which they are presented with the menu. They will select either 1, 2, or 0.

If they select 1, they will be converting a foreign currency to USD. Ask for the amount they'd like to convert, then ask for a character representing the currency they're converting from (either Y,y,P,p,or E,e -- Yen, Pounds, or Euros) -- allowing both uppercase and lowercase letter entries. Pass these values to your toUSD function to help to display the amount in USD.

If the user selects 2 on the menu, they will be adding two amounts in different currencies together to get one value in USD. Prompt them for the type of the first currency and the amount of the first currency, and the type and amount of the second currency. Then, display the first currency amount + the second currency amount in USD to the screen. You'll call your toUSD() function twice here to help with this.

If the user selects 0, allow the program to exit. They will be prompted for a menu entry as long as they enter other values besides 0. Only upon entering 0 should the program quit.

If the user selects something other than 1,2, or 0, you must print an error message. See the sample runs.

4. ERROR CHECKING: You must implement error checking on the following situations: When the user enters an invalid menu option (some integer value other than 1,2, or 0), display an error message as seen in the sample runs. Also, if a negative amount is entered at any time, display an error message as seen in the sample runs. You may ASSUME that the user enters a correct character for Yen, Pounds, and Euros when prompted, and you may assume the user will enter the correct type of data at all times (aka, they wont enter a char when they're prompted for the menu option or for an amount for instance).

5. Assume the conversion rates are as follows: \$1 USD is equal to: .71 Pounds, .8 Euros, and 110.05 Yen (conversion rates as of 2/2/18)

#### Sample Run 1:

(user input underlined)
```
Welcome to the currency exchange calculator!
Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
> 1
What amount would you like to convert? > 45.67
Which currency would you like to convert to USD?
P - Pound       E - Euro        Y - Yen
> P
45.67 Pounds converts to 64.32 US Dollars.

Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
> 2
Enter type of first currency    P - Pound       E - Euro        Y - Yen
> e
Enter amount of first currency
> 3.00
Enter type of second currency    P - Pound       E - Euro        Y - Yen
> P
Enter amount of second currency
> 3.00
This would equal 7.98 US dollars

Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
> 0
Goodbye!

```

#### Sample Run 2:

(user input underlined)
```Welcome to the currency exchange calculator!
Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
> 5
Invalid selection. Try again.

Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
> 0
Goodbye!

```

#### Sample Run 3:

(user input underlined)
```Welcome to the currency exchange calculator!
Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
> 1
What amount would you like to convert? > -876.5
Invalid amount entered. Amount must be non-negative.

Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
> 0
Goodbye!

```

#### Sample Run 4:

(user input underlined)
```Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
> 2
Enter type of first currency    P - Pound       E - Euro        Y - Yen
> e
Enter amount of first currency
> -8.09
Enter type of second currency    P - Pound       E - Euro        Y - Yen
> p
Enter amount of second currency
> 98.3
Invalid amount entered. Amounts must be non-negative.

Please select one of the following options:
1 - Convert foreign Amount to USD
0 - Quit
-----------------------------------
> 0
Goodbye!
```

### Requirements for all programs

• No global variables, other than constants
• There should be only one return statement from each function (this includes main() )
• If the use of functions is required, the tasks must be performed with the functions specified (not just with a single main() routine)
• Note that the seconds exercise requires the writing of a function, and a main routine to test that function.
• Each function should do exactly the task specified in the exercise
• In this assignment (Ex 2), main() should involve CALLING the function(s) you write, sometimes more than once
• Note that there is NO keyboard-input/screen-output specified in the functions themselves (i.e. "return" does not mean "print")
• All input and output must be done with streams
• You may use the iostream , iomanip , and cmath libraries (the ones that have been discussed in class)
``` numbers.cpp