Here is the full assignment for this "Making Change" program.
Write a program to input two float values. The first float is the cost of some purchase made or the cost of goods and the seconds is the payment value towards the cost. You are to determine the change(if any) to return to the customer. You must also determine if they gave you enough money to cover the cost and if they didn't print out you are short on funds so pay more.
The change should be in terms of 20 dollar bills, and/or number of 10's, 5's, and 1's in bills.
Then the change for coins, the number of quarters, dimes, nickels, and pennies. DO NOT print out 0 for any value.
Print out the inputs, 'cost' and 'payment' and then the amount of change to return. Print results to a file to print and turn in.
Example output: purchase: 0.81, pay: 1.00 change: 1 dime, 1 nickel, 4 pennies.
Test data for your program:
Input: 1,2,3,4,5,6
Cost: 0.81, 65.00, 16.76, 22.00, 0.25, 42.05
Payment: 1.00, 100.00, 50.00, 20.00, 100.00, 42.05
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
ofstream out;
out.open("MakeChange.out");
float cost, payment, change;
int total_pennies;
const int TWENTIES = 2000, TENS = 1000, FIVES = 500, ONES = 100,
QUARTER = 25, DIME = 10, NICKEL = 5,PENNY = 1;
cout << "You've entered the Make Change Program\n\n\n";
cout << "Enter the COST: ";
cin >> cost;
cout << "Enter the PAYMENT: ";
cin >> payment;
change = payment - cost;
if(payment < cost)
{
cout << "You need more money to purchase this item.";
}
if(cost == payment)
{
cout << "No change.";
}
else
{
cout << "\nYour change is: $ " << change;
total_pennies = static_cast<int>(change * 100);
int num_twenties = total_pennies / TWENTIES;
total_pennies %= TWENTIES;
if(num_twenties)
{
cout << "\nTWENTIES: " << num_twenties;
}
int num_tens = total_pennies / TENS;
total_pennies %= TENS;
if(num_tens)
{
cout << "\nTENS: " << num_tens;
}
int num_fives = total_pennies / FIVES;
total_pennies %= FIVES;
if(num_fives)
{
cout << "\nFIVES: " << num_fives;
}
int num_ones = total_pennies / ONES;
total_pennies %= ONES;
if(num_ones)
{
cout << "\nONES: " << num_ones;
}
int num_quarters = total_pennies / QUARTER;
total_pennies %= QUARTER;
if(num_quarters)
{
cout << "\nQUARTERS: " << num_quarters;
}
int num_dimes = total_pennies / DIME;
total_pennies %= DIME;
if(num_dimes)
{
cout << "\nDIMES: " << num_dimes;
}
int num_nickels = total_pennies / NICKEL;
total_pennies %= NICKEL;
if(num_nickels)
{
cout << "\nNICKELS: " << num_nickels;
}
int num_pennies =total_pennies / PENNY;
total_pennies %= PENNY;
if(num_pennies)
{
cout << "\nPENNIES: " << num_pennies;
}
}
out.close();
return 0;
}
| |
I believe I have this program 95% completed. I'm just confused on the inputs still. I don't want to create all const variables, as it would force a lot more code.
Whenever one writes to a file, is there a way to have CIN and it print to the file?