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
|
#include <iostream>
using namespace std;
void inp(int& total);//sets 'q_25,' 'q_10,' 'p_1,' equal to user's input
void calc(int& total, int& q_25, int& d_10, int& p_1);//converts to individual coins & calculates output
void outp(int& total, int& q_25, int& d_10, int& p_1);//outputs results in individual coins
int main()
{
char cont = 'y';
int total, q_25, d_10, p_1;
do//do-while loop allows program to be run multiple times
{inp(total);
calc(total, q_25, d_10, p_1);
outp(total, q_25, d_10, p_1);
cin >> cont;}
while(cont == 'y' || cont == 'Y');
return 0;}
void inp(int& total)//***INPUT***
{
cout << "Enter the total amount of cents in change that you are trying to get.\n";//prompt
//Take 'total'
cin >> total;
while(total < 0)//edge case if total is less than 0
{cout << "Please enter a positive whole number. Try Again.\n";
cin >> total;}
cout << " cents?\n"
<< total << " cents can be exchanged for...\n";}
void calc(int& total, int& q_25, int& d_10, int& p_1)//***CALCULATIONS***
{
//converts total cents to individual coins, and subtracts value of that coin from total amount
q_25 = 0;
d_10 = 0;
p_1 = 0;
while(total >= 25)//loops until no more quarters can be taken from total
{total = total - 25;//subtracts value of coin from total
q_25 = q_25 + 1;}//for every coin value removed, add one to the number of coins
while(total >= 10)//loops...dimes
{total = total - 10;
d_10 = d_10 + 1;}
while(total >= 1)//loops...pennies(or any coins)
{total = total - 1;
p_1 = p_1 + 1;}}
void outp(int total, int q_25, int d_10, int p_1)//***OUTPUT***
{
cout << "Your total amount of change is " << q_25;
if (q_25 == 1)
cout << " quarter, ";
else
cout << " quarters, ";
cout << d_10;
if (d_10 == 1)
cout << " dime, ";
else
cout << " dimes, ";
cout << "and " << p_1;
cout << p_1;
if (p_1 == 1)
cout << " penny.\n\n";
else
cout << " pennies.\n\n";
cout << "Do you want to continue? < 'y' or 'n' >: ";}
|