In this post I will discuss altering the flow of control with
if/else statements. These statements ask the computer to evaluate whether a condition is
false and changes the flow of control based on the answer. Along the way I discuss another data type,
bool, which can hold either true or false and I show a few examples of how to use selection with
if/else if/else, and
Now that you have reviewed the guided code walk-throughs I ask that you write a few programs:
Problem 1 asks you to write a program to determine if one date comes after another. The program will ask for two sets of dates. Next, the program will determine if the first date comes before, is equal to, or comes after.
Enter in the first month: 2 Enter in the first day: 21 Enter in the first year: 2012 Enter in the second month: 2 Enter in the second day: 22 Enter in the second year: 2011 The first date comes after the second.
Problem 2 asks you to write a programs that prompts the user for a date and determines if that date is valid. For example, 9/19/2017 is a valid date but these are not valid dates:
4/31/2006 (only 30 days in April)
2/29/2005 (not a leap year)
16/1/2010 (invalid month)
4/59/2013 (invalid day)
If the date is correct print it out. If it is incorrect display an error message explaining why the date is not correct.
Problem 3 asks you to write a program that will calculate change for a sales purchase. Your program should prompt for a sales price. Validate that the data entered is a number greater than 0. If the data entered is incorrect, display an error message and end the program. Next, prompt the user for the amount that the customer will pay to the cashier. Validate that this value is greater than or equal to the sales price. If it is not, display an error message and end the program.
If the entry is correct, your program must calculate the amount of change to return to the user. Next, calculate what bills and coins that the the cashier needs to return to the customer. The fewest number of paper bills and coins should be returned. You can make change in many different combinations, however, the only correct implementation is the one that returns the fewest paper bills and coins.
Display the number of each of the bills and coins. Here is a sample run of the program:
Enter in a sales amount: $20.38 Enter in the amount the customer pays: $30.00 The change due back is $9.62 You should give the customer this change: 0 $100 bills 0 $50 bills 0 $20 bills 0 $10 bills 1 $5 bills 4 $1 bills 1 Half Dollars 0 Quarters 1 Dimes 0 Nickels 2 Pennies
Because of the way arithmetic works with float variables, storing the monetary values as floats may cause some problems. For example, if you had a float variable that held 1.29 to represent $1.29 and you subtracted the .05 from it (to represent giving back a nickel) you would think that you would be left with exactly 1.24. Unfortunately, the computer might store that value or it might store 1.2399999 or 1.2400001 instead of exactly 1.24. These very small inconsistencies can cause a problem calculating the number of pennies to return. Consider converting the amounts into ints to solve this problem.
You can find all of these code playbacks in my free 'book', An Animated Introduction to Programming in C++. I am always looking for feedback so please feel free to comment here or to send me a message. You can follow me on twitter @markm208.