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.

Task

Write the following programs, each in a separate file. You should also create seperate projects in VS for each of these. Filenames should be: (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:

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


Submitting:

Submit only your source code files through the canvas submission. As there are two of them, this will require browsing your computer for 2 files to upload at one time. DO NOT upload your files in seperate canvas submissions. Submit the files:
  • days.cpp
  • coingame.cpp
  • in one canvas submission.