Making program more efficient
Mar 22, 2016 at 12:00am UTC
I wanted to get some feedback on a program I just finished. It's pretty much a cash register and I wanted to know if there is a more efficient way to determine how change is given back(dollars, quarters, dimes, ect...)
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// initiated variables
double amountOwed = 0.0;
double amountPaid = 0.0;
int dollarBills = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
double amountStillOwed = 0.0;
double amountOfChange = 0.0;
// enter variables
cout << "How much was owed?" << endl;
cin >> amountOwed;
cout << "How much was paid?" << endl;
cin >> amountPaid;
// calculate whether there is an amount still owed or amount of change ad display it
if (amountOwed > amountPaid)
{
amountStillOwed = amountOwed - amountPaid;
cout << "This is the amount still owed: $" << amountStillOwed << endl;
}
else
{
amountOfChange = amountPaid - amountOwed;
cout << "This is the amount of change: $" << amountOfChange << endl;
}
// if statements determine how the change is given back
if (amountOfChange >= 1.00)
{
dollarBills = amountOfChange / 1.00;
amountOfChange -= (dollarBills * 1.00);
}
if (amountOfChange >= .25)
{
quarters = amountOfChange / .25;
amountOfChange -= (quarters * .25);
}
if (amountOfChange >= .10)
{
dimes = amountOfChange / .10;
amountOfChange -= (dimes * .10);
}
if (amountOfChange >= .05)
{
nickels = amountOfChange / .05;
amountOfChange -= (nickels * .05);
}
if (amountOfChange >= .00)
{
pennies = amountOfChange / .01;
amountOfChange -= (pennies * .01);
}
// displays the amount and type of change
cout << "This is the change that should be returned: " << endl;
cout << "Dollar bills: " << dollarBills << endl;
cout << "Quarters: " << quarters << endl;
cout << "Dimes: " << dimes << endl;
cout << "Nickels: " << nickels << endl;
cout << "Pennies: " << pennies << endl;
return 0;
}
Topic archived. No new replies allowed.