Hello everyone I am in need of some help.. I thought that I did my assignment the way that my professor wanted but I just found out that he wants me to solve it differently.
Problem:
Given any amount of money expressed in dollars, and cents, this program computes the number of 100 , 50, 20, 10, 5 and 1 dollar bills and number of quarters, dimes, nickels, and pennies to be returned, returning how many of each denomination are included.

I figured out how to program it correctly for the most part except I cannot figure out how to handle large numbers, such as 14.49999999999999999999999999999999.
When converting that number to int by multiplying by 100 it returns it as 1450 not 1449. Does anyone know how to handle converting doubles, similar to the one above, to an integer correctly?

This is the code that my teacher did not want, since I handled the input as a string:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include #include using namespace std; int main(){ const int hundredsConst=10000, fiftiesConst=5000, twentiesConst=2000, tensConst=1000; const int fivesConst=500, onesConst=100, quartersConst=25, dimesConst=10, nickelsConst=5, tab=5; int hundreds, fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies; int dollarsBeforeDecimal, centsAfterDecimal, moneyInPennies, counter=1; string name, hundredsEnd, fiftiesEnd, twentiesEnd, tensEnd, fivesEnd, onesEnd, quartersEnd, dimesEnd, nickelsEnd, penniesEnd; char decimalPoint; cin >> dollarsBeforeDecimal >> decimalPoint >> centsAfterDecimal; moneyInPennies = (dollarsBeforeDecimal*100) + (centsAfterDecimal); hundreds = moneyInPennies/hundredsConst; moneyInPennies -= hundreds*hundredsConst; fifties = moneyInPennies/fiftiesConst; moneyInPennies -= fifties*fiftiesConst; twenties = moneyInPennies/twentiesConst; moneyInPennies -= twenties*twentiesConst; tens = moneyInPennies/tensConst; moneyInPennies -= tens*tensConst; fives = moneyInPennies/fivesConst; moneyInPennies -= fives*fivesConst; ones = moneyInPennies/onesConst; moneyInPennies -= ones*onesConst; quarters = moneyInPennies/quartersConst; moneyInPennies -= quarters*quartersConst; dimes = moneyInPennies/dimesConst; moneyInPennies -= dimes*dimesConst; nickels = moneyInPennies/nickelsConst; moneyInPennies -= nickels*nickelsConst; pennies = moneyInPennies; return 0; }``````
Last edited on
Sorry for posting this under this subject by the way.. I think it should probably go under beginners.
closed account (3qX21hU5)
Have you learned about loops yet? If so this would be a great time to get used to them and use them on a assignment.

For example lets say we have a dollar amount of \$2.54 cents.

We could have a loop that runs for each bill or change value starting with the largest and moving to the lowest (\$1, \$.25, \$.10, \$.05, \$.01) and subtracting from the total amount and adding to 1 on each pass through the loop to their respective variables.

So with that logic we would have a variable for the dollar amount that the user enters (\$2.54) and a variable for each of the of dollars, quarters, dimes, ect.

Once we have that we would start with the loops. For example if we the largest dollar bill we tracked was the \$1 bill this would be how the first loop could look.

 ``123456789`` ``````double amount = 2.54; int dollars = 0; while (amount >= 1.00) { amount -= 1.00; ++dollars; }``````

So after that finishes running the variable double would equal 0.54 and the variable dollars would equal 2. We would then move on to the next lowest which would be quarters and repeat the process.
Last edited on
sorry but I forgot to mention that our professor does not want us to use more "advanced" techniques like while loops, if statements, etc for this problem since we have just started the semester. He says it should be able to be programmed using just mathematical functions.
Thanks though for your help and time, apologize for making you type all that out =\.
I'm not sure if your teacher allows use code outside of your given headers, if so take a look at <cmath> (math.h) at http://www.cplusplus.com/reference/cmath/.

If not, you've already stumbled on a way to do what you're looking for.

When you multiplied your double/float (14.49~) by an int, it was converted to an int. When converting to an int, the compiler always rounds up.

You can convert to int in ways other than multiplying by an int. for example :
 ``1234567`` ``````double random_number = 123.456; int x = 0; x = random_number; //OR a more simplified version double random_number = 123.456; random_number = (int)random_number; //This would turn 123.456 into 13 ``````

Hope this sets you on the right track.
Last edited on
No, conversion of float to integer drops the reminder, i.e. rounds down for positive values.
 ``12`` ``````int x = 100 * 14.4999; assert( 1449 == x );``````

 ``12`` ``````cin >> dollarsBeforeDecimal >> decimalPoint >> centsAfterDecimal; moneyInPennies = (dollarsBeforeDecimal*100) + (centsAfterDecimal);``````

 ```14.4999 => 14 and 4999 14*100 + 4999 = 6399 cents```

Not quite the expected 1449 cents.
Last edited on
Topic archived. No new replies allowed.