-
Output and Control
I've been working on a problem that it just boggling my mind. I need to be able to convert a given number into change (i.e. quarters, dimes, nickels, pennies.) so if i type in 3.50 i get 14 quarters, if i type in 3.45 i get 13 quarters and two dimes etc. what i have so far is each number individually (i.e 3.50 gets me 14 quarters, 35 dimes, 70 nickels, 350 pennies) the question i have is, how am i able to do this? this is what i have so far
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
float amount;
float quarters;
float dimes;
float nickels;
float pennies;
cout << "I will convert your money into change. \n\n";
cout << "Enter the amount:";
cin >> amount;
quarters = amount/.25f;
cout << "You recieve " << quarters << "Quarters.\n\n" ;
dimes = amount/.10f;
cout << "You recieve " << dimes << " Dimes.\n\n" ;
nickels = amount/.05f;
cout << "You recieve " << nickels << " Nickels.\n\n" ;
pennies = amount/.01f;
cout << "You recieve " << pennies << " Pennies.\n\n" ;
return 0;
}
this code does each coinage individually, but i need them to be unified
ive tried many different things so far, to no avail.
-
You must update your total amount after each conversion:
Code:
quarters = amount / .25f;
amount -= quarters * .25f;
dimes = amount / .10f;
amount -= dimes * .1f;
etc...
-
another problem... i guess it automatically rounds up? so u end up with 0 after u subtract teh quarters
-
ooh i got it
Code:
quarters = amount/.25f;
amount -= quarters *.25f;
cout << "You recieve " << quarters << " Quarters.\n\n" ;
dimes = amount/.10f;
cout << "You recieve " << dimes << " Dimes.\n\n" ;
amount -= dimes *.10f;
nickels = amount/.05f;
cout << "You recieve " << nickels << " Nickels.\n\n" ;
amount -= nickels *.05f;
pennies = amount/.01f;
amount -= pennies *.01f;
cout << "You recieve " << pennies << " Pennies.\n\n" ;
also note that i changed the type of variable from float to int, seeing as there is no such thing as half a quarter/dime/nickel/penny :p