# My pennies keep falling short by 1 cent... Any pointers

 Jose Cruz · April 15, 2015 Using Visual Studio. This code is for a homework assignment. I thought I had it all figured out. When I did the final build and ran the program, everything generated correctly but the pennies. It calculates the pennies at 1 cent short of the value needed. so for the first one of 1.88 it prints:7 quarters1 dime0 nickles2 pennies (it should be 3 pennies)It does the same for any input I try.Her is the code``#define _CRT_SECURE_NO_WARNINGS#include #include void change(float, int *, int*, int*, int*);int main(){ float total; int quarters, dimes, nickels, pennies; { total = 1.88; change(total, &quarters, &dimes, &nickels, &pennies); printf("\nTOTAL VALUE ENTERED: %6.2f.\n", total); printf("\n%d quarters", quarters); printf("\n%3d dimes", dimes); printf("\n%3d nickels", nickels); printf("\n%3d pennies", pennies); } { total = 0.32; change(total, &quarters, &dimes, &nickels, &pennies); printf("\nTOTAL VALUE ENTERED: %6.2f.\n", total); printf("\n%d quarters", quarters); printf("\n%3d dimes", dimes); printf("\n%3d nickels", nickels); printf("\n%3d pennies", pennies); } { printf("\nEnter the amount of change in your pockets:  "); scanf("%f", &total); change(total, &quarters, &dimes, &nickels, &pennies); printf("\nTOTAL VALUE ENTERED: %6.2f\N", total); printf("\n%d quarters\n", quarters); printf("\n%3d dimes\n", dimes); printf("\n%3d nickels\n", nickels); printf("\n%3d pennies\n", pennies); } return 0;}void change(float total, int *quarters, int *dimes, int *nickels, int *pennies){ if (total >= 0.25) *quarters = (total / 0.25); if (total >= 0.10) *dimes = (total - (*quarters * 0.25)) / 0.10; if (total >= 0.05) *nickels = (total - (*quarters * 0.25) - (*dimes * 0.10)) / 0.05; if (total >= 0.01) *pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05)) / 0.01; return;}``

## Replies

 Dol Lod · April 16, 2015 I assumed that your change function wasn't written correctly and that changing it might help. Now, I have some good news and some bad news.The bad news is that I have a theory about why it is not working. Floats don't have enough precision for what you want to do so the fractional values are never going to be accurate enough to get this to work since they don't have enough significance. I tried the same thing with doubles. The good news is that your change function definitely works. My advice to fix this problem is to change to the double type. Then always multiply by 100. Then you are doing integer addition which is defined. Remember, integers work perfectly so enter the information as a double, multiply by 100 to make it an integer and then calculate. To fix this, I altered the type from float to double. The next change I made was make the change function use clean division by changing the inputted total to an integer by mutliplying by 100 like  I said.  Then I used simple integer division. ``#define _CRT_SECURE_NO_WARNINGS#include void change(double, int *, int*, int*, int*);int main(){ double total; int quarters, dimes, nickels, pennies; total = 1.88; change(total, &quarters, &dimes, &nickels, &pennies); printf("\nTOTAL VALUE ENTERED: %6.2f.\n", total); printf("\n%d quarters", quarters); printf("\n%3d dimes", dimes); printf("\n%3d nickels", nickels); printf("\n%3d pennies", pennies); total = 0.32; change(total, &quarters, &dimes, &nickels, &pennies); printf("\nTOTAL VALUE ENTERED: %6.2f.\n", total); printf("\n%d quarters", quarters); printf("\n%3d dimes", dimes); printf("\n%3d nickels", nickels); printf("\n%3d pennies", pennies); printf("\nEnter the amount of change in your pockets:  "); scanf("%f", &total); change(total, &quarters, &dimes, &nickels, &pennies); printf("\nTOTAL VALUE ENTERED: %6.2f\N", total); printf("\n%d quarters\n", quarters); printf("\n%3d dimes\n", dimes); printf("\n%3d nickels\n", nickels); printf("\n%3d pennies\n", pennies); return 0;}void change(double total, int *quarters, int *dimes, int *nickels, int *pennies){ if(total<0) return; total*=100; *quarters=total/25; total-=25*(*quarters); *dimes=total/10; total-=10*(*dimes); *nickels=total/5; total-=5*(*nickels); *pennies=total;}`` Dol Lod · April 15, 2015 Please use code tags. This is very hard to read. They do work if you edit the original post. Additionally, wouldn't it be simpler to use a series of while loops, subtracting from the total each time while incrmenting the number of quarters. I would also floor the result of division if you want to do it that way and then subtract from tottal. Jose Cruz · April 16, 2015 tbh, I am still a beginner and not too familiar with the lingo... So your saying in the function i should use while loops? Also, how is that going to change the result of the pennies?
• 1

## C

130,029 followers