I was given an assignment where I ask the user for an amount in $0000.00 then tell him that amount in coins.
for example $2500.25 would be 100001 quarters, 0 dimes, 0 nickles, 0 pennies
here is my code, but it seems I can't workout the pennies right
if i input an amount like 2500.76, it says 0 pennies except there is actually one penny.
#include <iostream>
usingnamespace std;
int main ()
{
double total; // amount of user input
int quarters; // amount of quarters
int pennies; // amount of pennies
int dimes; // amount of dimes
int nickles; // amount of nickles
cout << "Enter an amount of US Dollars in the format: "; //Prompt User for Amount
cin >> total; // Input Amount
pennies = static_cast<int>(total * 100); // convert floating point amount into (pennies) integer
quarters = pennies / 25; // how many quarters
dimes = (pennies % 25)/10; // how many dimes left
nickles = (pennies % 25 % 10) / 5; // how many nickles left
pennies = (pennies % 25 % 10 % 5 % 1) / 1; // how many pennies left
cout << "The amount of coin is: \n"<< quarters << " quarters, \n"; // output amount into coins
cout << dimes << " dimes, \n";
cout << nickles << " nickle, \n";
cout << pennies << " pennies. \n";
} // end main
This is just a wild guess, but it might have to do with the order in which the % operator is processed. You're implicitly assuming it is handled from left to right, but that might not be the case.
@ Disch
here is the change, now 2500.76 works, but if enter an amount like 1234.56, it gives me a pennies when it suppose to be 0, im not sure if i followed your advice correctly
1 2 3 4 5 6 7 8 9 10 11 12
pennies = static_cast<int>(total * 100); // convert floating point amount into (pennies) integer
quarters = pennies / 25; // how many quarters
pennies -= quarters * 25;
dimes = (pennies % 25)/10; // how many dimes left
pennies -= dimes * 10;
nickles = (pennies % 25 % 10) / 5; // how many nickles left
pennies -= nickles * 5;
pennies = (pennies % 25 % 10 % 5) / 1; // how many pennies left
@Disch yea i made a mistake, you're right there is a penny
my teacher insists we use % in our assignment
I followed your second example and it worked, thank you very much!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
pennies = static_cast<int>(total * 100); // convert floating point amount into (pennies) integer
quarters = pennies / 25; // how many quarters
pennies %=25;
dimes = (pennies % 25)/10; // how many dimes left
pennies %=10;
nickles = (pennies % 25 % 10) / 5; // how many nickles left
pennies %=5;
i realize if i took out the two 1s from my old code, it works also,
what do you think? i know its less efficient this way but when i enter 1234.56 or 2500.76 it got the answers right
1 2 3 4 5 6 7 8 9 10 11 12 13 14
pennies = static_cast<int>(total * 100); // convert floating point amount into (pennies) integer
quarters = pennies / 25; // how many quarters
dimes = (pennies % 25)/10; // how many dimes left
nickles = (pennies % 25 % 10) / 5; // how many nickles left
pennies = (pennies % 25 % 10 % 5); // how many pennies left