Assignment #2 - Using Selection Statements

Due: Wednesday January 31st, 11:59:00pm

Objective

This assignment will consist of writing two smaller programs, which will give practice with basic selection statements, as well as further practice with basic I/O.

Write the following programs, each in a separate file. You should also create seperate projects in VS for each of these. Filenames should be:
• days.cpp
• coingame.cpp
(Note that the filenames are all lowercase)

Exercise 1

Write a program (filename days.cpp) that does the following:

Prompt the user for a month (1-12) and a year (both of these should be stored as ints). If the user enters an invalid month (outside the range of 1-12) you should print an error message and not print out the number of days. (See sample run 3). You may assume the user enters a valid year. If they do enter a valid month: then, the program should display the number of days in that month. For February, you'll need to consider leap years. Use the following rules: Determine if the year is divisible by 100. If it is, then the year is a leap year if and only if the year is also divisible by 400. For example, 2000 is a leap year but 2100 is not. If the year is NOT divisible by 100, then it is a leap year if and only if it is divisible by 4. For example, 2008 is a leap year but 2009 is not.

Sample run: (user input underlined)

```Enter a month (1-12): 1
Enter a year: 2010

Number of days: 31
```

Sample run 2: (user input underlined)

```Enter a month (1-12): 2
Enter a year: 2008

Number of days: 29
```

Sample run 3: (user input underlined)

```Enter a month (1-12): 15
Enter a year: 1989

Invalid Month entered. Aborting Program.
```

Exercise 2

Filename: coingame.cpp (Based off of programming exercise 9 in Chapter 4)

This program will play a game with the user in which they will either lose or win. They win if they can give you a breakdown of an amount of half dollars, quarters, dimes, nickels, and pennies that adds up to whatever amount they specify when the program begins.

Write a program that does the following:

• Welcome the user to the game and ask them if they want to play. They should type y or n for yes or no. Store this value in a char variable. If they type y, continue on with the game. If they type n, print the appropriate message and exit (See sample runs). If they type some other character, print the error message (see sample runs) and abort the program).
• If the user said they wanted to play continue with the game as such: prompt them for a dollar amount (as a double) that they will attempt to match with their coin entries.
• Allow the user to enter an integer amount for the number of half dollars, quarters, dimes, nickels, and pennies that they'd like. If any of the values they enter for the number of each coin is negative, display an error message and exit the program. (You may ONLY have one return statement in your code - the one at the end of main, and no exit statements are allowed. This means you need to structure if/elseif/else stuff so that your code will naturally flow to the end if an error condition is encountered).
• If all of the values they entered are valid, check to see if the user won the game. Add up the total amount of \$ they entered and compare it against the value they entered when the program began.
• If it's the same and they won, print out: "YOU WIN!". If they lost, print out "YOU LOST by: " and then the amount of \$ that they were off by
• You may assume the user will enter a valid dollar amount in the form of dd.cc when asked what \$ amount they want to match in the beginning of the program
• All dollar values printed must be printed in dollar format like \$dd.cc (to two decimal places).

Sample Runs

(user input is underlined, to distinguish it from output) Remember your output format should look EXACTLY like mine...

Sample run 1

```-----COP3014 Coin Game-----
Would you like to play? (y or n) --> y
What amount of \$ would you like to match? --> 2.00
Okay! You need to match the value of \$2.00
Enter the number of:
Half Dollars: 2
Quarters:  2
Dimes:  5
Nickels:  0
Pennies:  0
You WIN!
See you next time!
```

Sample run 2

```-----COP3014 Coin Game-----
Would you like to play? (y or n) --> y
What amount of \$ would you like to match? --> 1.50
Okay! You need to match the value of \$1.50
Enter the number of:
Half Dollars: 0
Quarters:  0
Dimes:  0
Nickels:  0
Pennies:  150
You WIN!
See you next time!
```

Sample run 3

```-----COP3014 Coin Game-----
Would you like to play? (y or n) --> y
What amount of \$ would you like to match? --> 1.23
Okay! You need to match the value of \$1.23
Enter the number of:
Half Dollars: 2
Quarters:  1
Dimes:  0
Nickels:  0
Pennies:  -2
You can't enter a negative amount of coins. Aborting game.
```

Sample run 4

```-----COP3014 Coin Game!-----
Would you like to play? (y or n) --> y
What amount of \$ would you like to match? --> 2.00
Okay! You need to match the value of \$2.00
Enter the number of:
Half Dollars: 6
Quarters:  20
Dimes:  1
Nickels:  2
Pennies: 4
You LOST by: \$6.24
See you next time!
```

Sample run 5

```-----COP3014 Coin Game-----
Would you like to play? (y or n) --> y
What amount of \$ would you like to match? --> 4.00
Okay! You need to match the value of \$4.00
Enter the number of:
Half Dollars: 0
Quarters:  0
Dimes:  0
Nickels:  0
Pennies:  1
You LOST by: \$3.99
See you next time!
```

Sample run 6

```-----COP3014 Coin Game-----
Would you like to play? (y or n) --> n
Guess you're scared! Come back when you're ready to play!
```

Sample run 7

```-----COP3014 Coin Game-----
Would you like to play? (y or n) --> z
Invalid entry, aborting program.
```

Requirements for all programs

• You may ONLY HAVE ONE return statement which is the last line of main() in each of your mini-programs. No exit statements are permitted, either.
• No global variables
• All input and output must be done with streams, using the library iostream
• You may only use the iostream library and the iomanip library. (you do not need any others for these tasks)