Using C++ Language, I want to create a program using everything up to arrays...
ID: 3572107 • Letter: U
Question
Using C++ Language, I want to create a program using everything up to arrays... the shorter the program the better
You are at my store and I am a stickler for making sure the cash drawer is exactly correct each night. I need a program that takes in two inputs:
1) The total amount of any sale (tax and any discounts already included)
2) The amount of money the customer gave you
The program will then calculate and display the exact amount of change to be given back to the customer.
Not so easy; the display will tell you exactly how many of each of the following to give to the customer: Dollars: 20, 10, 5 and 1 Change: quarters, dimes, nickels and pennies
In other words, for the money denominations listed above, your program must calculate and display the number of each denomination (if needed).
Example:
Amount of sale: $23.12
Amount tendered: $50.00
The change is . . . $26.88
1 twenty dollar bill
1 five dollar bill
1 one dollar bill
3 quarters
1 dime
3 pennies
Explanation / Answer
#include <iostream>
using namespace std;
struct Denom {
const int cents;
const char *name;
};
//array of strucure to hold each cents detail and name
const Denom DenomArray[] = {
{ 10000, "Hundred-Dollar Bills" },
{ 5000, "Fifty-Dollar Bills" },
{ 2000, "Twenty-Dollar Bills" },
{ 1000, "Ten-Dollar Bills" },
{ 500, "Five-Dollar Bills" },
{ 100, "One-Dollar Bills" },
{ 25, "Quarters" },
{ 10, "Dimes" },
{ 5, "Nickels" },
{ 1, "Pennies" }
};
int main() {
double changeAmount,Amtsales,TenAmt;
cout<<"Amount of sale: $";
cin>>Amtsales; //accept sale amount
cout<<"Amount tendered: $";
cin>>TenAmt; //accept tendered amount
changeAmount=TenAmt-Amtsales;//find change amount
cout<<"The change is . . . "<<changeAmount<<endl;
//Convert change
int cents = 100 * changeAmount;
for (int i = 0; i < sizeof(DenomArray) / sizeof(DenomArray[0]); ++i) {
if((cents / DenomArray[i].cents)!=0)
{
cout<<(cents / DenomArray[i].cents) <<" "<<DenomArray[i].name <<' ';
}
cents %= DenomArray[i].cents;
}
}
==================================================
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Amount of sale: $23.12
Amount tendered: $50.00
The change is . . . 26.88
1 Twenty-Dollar Bills
1 Five-Dollar Bills
1 One-Dollar Bills
3 Quarters
1 Dimes
3 Pennies
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.