You have declared num to be an int, so when you input 2.36, only the 2 will be assigned to num. num is an integer, not a floating point variable.
If you want a simple way to input a floating point number, try:
- Code: Select all
// Always initialize your variables!
double num = 0.0;
int cents = 0, quarters = 0, dimes = 0, nickles = 0;
cout << "The program will calculate the number "
"of quarters, dimes, nickles, and pennies to generate "
"the number of cents entered.\n";
cout << "Enter a number in dollars.\n";
cin >> num;
// Just think about why this will work...
cents = num*100;
// 2.36 * 100 == 236 (or 236 cents, which is what you want)
That should take care of that problem.
Your other problem is that your calcualtions are wrong.
You need to think through the problem again.
You have:
quarters = cents%25;
cents = quarters * 25;
So for the 2.36 example, 2.36 goes to 236 (from the code above). Now, what's the remainder when you divide 236 by 25? What is the quotient?
236 / 25 = 9
236 % 25 = 11
Think about which one is the number of quarters you'll have, and which one is how many cents you have left over.