//
// change.cpp
// PA04 - Change
//
// Created by Chad Spensky on 7/26/16.
// Copyright © 2016 Chad Spensky. All rights reserved.
//
#include
using namespace std;
void compute_coins(int coin_value, int& num, int& amount_left);
// Precondition: 0 < coin_value < 100; 0 <= amount_left < 100.
// Postcondition: num has been set equal to the maximum number
// of coins of denomination coin_value cents that can be obtained
// from amount_left. Additionally, amount_left has been decreased
// by the value of the coins, that is, decreased by
// num * coin_value.
int main(int argc, const char * argv[]) {
// Loop until the user terimates the program with 0
while (true) {
// Declare our variables
int number_of_cents = 0;
int cents_remaining = 0;
int quarters = 0;
int dimes = 0;
int pennies = 0;
cout << "Enter number of cents (or zero to quit):" << endl;
cin >> number_of_cents;
// Did the user exit?
if (number_of_cents == 0) {
break;
}
// Decrement cents remaining to 0, counting the number of coins used along the way
cents_remaining = number_of_cents;
// Quarters
compute_coins(25, quarters, cents_remaining);
// Dimes
compute_coins(10, dimes, cents_remaining);
// Pennies
compute_coins(1, pennies, cents_remaining);
// cent or cents?
if (number_of_cents > 1) {
cout << number_of_cents << " cents can be given as ";
} else {
cout << number_of_cents << " cent can be given as ";
}
// Proper english output for quarters
if (quarters > 1) {
cout << quarters << " quarters";
} else if (quarters == 1) {
cout << quarters << " quarter";
}
// Need a comma?
if (quarters > 0 && (dimes > 0 || pennies > 0)) {
cout << ", ";
}
// Proper english output for dimes
if (dimes > 1) {
cout << dimes << " dimes";
} else if (dimes == 1) {
cout << dimes << " dime";
}
// Need a comma?
if (dimes > 0 && pennies > 0) {
cout << ", ";
}
// Proper english output for pennies
if (pennies > 1) {
cout << pennies << " pennies";
} else if (pennies == 1) {
cout << pennies << " penny";
}
// Put our final period.
cout << "." << endl;
}
}
void compute_coins(int coin_value, int& num, int& amount_left) {
// Loop until the amount left is less than 1 coin
while (amount_left >= coin_value) {
// Remove the amount of 1 coin from the total value
amount_left -= coin_value;
// Count how many coins
num++;
}
}