Hi everyone, I am very new to programming, and don't know much yet. I have received a homework assignment in which I need to have a user input the total cost of something they are purchasing, and then input the amount they are tendering to pay for it. The output is meant to tell them how much they will receive back as change, and tell them which bills and coins they will receive. The assumptions that can be made, according to the instructions for the assignment, is that the total cost will never exceed 50 dollars, and that the customer will pay in whole dollar amounts, never with any cents (i.e., $5.00, $8.00, etc.), and that they will never tender more than $100.
My problem is that when I was testing the code, I put in the value 25.17 as the cost, and 50 as what was tendered. The code said that the customer is owed 24.83 as change, and that they would get 1 twenty dollar bill, 4 one dollar bills, 3 quarters, 1 nickel, and 2 pennies (and 0 of everything else). It should be 3 pennies. I have an idea that the problem was caused by placing values of type double into type int variables, but I am not totally sure on that. I'm not confident I know why the problem is occurring, and I don't know how to fix it. Can someone please provide me with a push in the right direction, or some suggestion on how to go about fixing this?
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
|
#include <iostream>
using namespace std;
// Declaring constants for dollar values
const int FIFTY = 50;
const int TWENTY = 20;
const int TEN = 10;
const int FIVE = 5;
const int ONE = 1;
// Declaring constants for cent values
const double QUARTER = 0.25;
const double DIME = 0.10;
const double NICKEL = 0.05;
const double PENNY = 0.01;
int main() {
double amtDue, // What customer owes
tendered, // What customer paid with
chgDue; // What change is due back to customer
cout << "What is the total cost of the item(s) you are purchasing? ";
cin >> amtDue;
cout << "\nWhat is the cash you are tendering to make your purchase? ";
cin >> tendered;
chgDue = tendered - amtDue; // Calculating total change due back to customer
double chgDueClone = chgDue; // Creating a variable to hold what variable chgDue holds, for later use
// Declaring int variables to truncate change due to the customer to determine the quantity of cash denominations to be returned
// Re-calculates the change due after each step
int fifties = chgDue / FIFTY;
chgDue = chgDue - (fifties * FIFTY);
int twenties = chgDue / TWENTY;
chgDue = chgDue - (twenties * TWENTY);
int tens = chgDue / TEN;
chgDue = chgDue - (tens * TEN);
int fives = chgDue / FIVE;
chgDue = chgDue - (fives * FIVE);
int ones = chgDue / ONE;
chgDue = chgDue - ones;
int quarters = chgDue / QUARTER;
chgDue = chgDue - (quarters * QUARTER);
int dimes = chgDue / DIME;
chgDue = chgDue - (dimes * DIME);
int nickels = chgDue / NICKEL;
chgDue = chgDue - (nickels * NICKEL);
int pennies = chgDue / PENNY;
chgDue = chgDue - (pennies * PENNY);
// Display the output, telling the customer the total amount they are due, and the quantities of certain bill denominations and coins they will receive
cout << "\nYou are due a total of $" << chgDueClone << endl << endl;
cout << "You will receive:\n"
<< fifties << " fifty dollar bills\n"
<< twenties << " twenty dollar bills\n"
<< tens << " ten dollar bills\n"
<< fives << " five dollar bills\n"
<< ones << " one dollar bills\n"
<< quarters << " quarters\n"
<< dimes << " dimes\n"
<< nickels << " nickels\n"
<< pennies << " pennies\n";
return 0;
} // End main
|