I'm at yet another exercise in my book:I'm not that far into the program - but I've hit a bit of a snag already. Here is my code:Code:Create a program
that prompts the user to enter an amount of money between $0
and $10(decimal places aloud). Determine how many quarters(25c)
dimes(10c) nickels(5c) and pennies(1c) are needed to make up that
amount. Output this information to the screen and ensure the
output makes gramatical sense.
The problem lies somewhere with the variable 'cash' of type double. When I output it to the screen after the program's done calculating how much money is left to work with after the number of quarters has been found - the value of change is printed to the screen as '0'. However, the number of quarters comes out fine. I'm very confused!Code:#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
double cash = 0;
const double quarter = .25;
const double dime = .10;
const double nickel = .05;
const double penny = .01;
double quarters = 0;
double dimes = 0;
double nickels = 0;
double pennies = 0;
double change = 0;
cout <<"Please enter an amount of money from $0 dollars, to $10 dollars: ";
cin >> cash;
cout << endl;
if((cash >= 0) && (cash <= 10)){
quarters = cash / quarter;
change = quarters * quarter;
cash = cash - change;
cout << quarters;
cout << endl;
cout << cash;
cout << endl;
}
else{
cout <<"You entered an invalid amount of money: ";
cout << cash;
cout << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Here is the program's logic - just so we all know what's supposed to happen - the user enters an amount of money... then the program finds out how many quarters there are in the amount - the value of all the quarters is added up and stored as 'change'.. which is then subracted from cash to come up with the amount of money minus the quarters... which is 'cash'.
I'll take that an apply it with dimes, nickels, and pennies as well - but that's after we all figure out why cash is displayed as 0! My guess is that I made a stupid mistake with the arithmatic that I fail to see.