Making a change counting program
Write a C function named change() that accepts a floating point number of total coins and the addresses of the integer variables named quarters, dimes, nickels, and pennies. The function should determine the number of quarters, dimes, nickels, and pennies in the total coins number passed to it and write these values directly into the respective variables declared in its calling function using pointers.
With that being said, I am following the directions in the assignment, so don't tell me to write this 5 other ways.
My issue is this. I have written the program, as shown below, and it all works EXCEPT that when I enter in a value of .24 and lower, the change count is incorrect.
Code:
#include <stdio.h>
int change (float total, int *quarters, int *dimes, int *nickels, int *pennies);
int main ()
{
int quarters, dimes, nickels, pennies;
float total;
{
total = 1.88;
change (total, &quarters, &dimes, &nickels, &pennies);
printf("TOTAL VALUE ENTERED: $%.2f", total);
printf("\n%3d quarters\n", quarters);
printf("\n%3d dimes\n", dimes);
printf("\n%3d nickels\n", nickels);
printf("\n%3d pennies\n", pennies);
}
{
total=0.32;
change (total, &quarters, &dimes, &nickels, &pennies);
printf("\nTOTAL VALUE ENTERED: $%.2f",total);
printf("\n%3d quarters\n",quarters);
printf("\n%3d dimes\n", dimes);
printf("\n%3d nickels\n", nickels);
printf("\n%3d pennies\n",pennies);
}
{
printf("\nPlease enter an amount of money: \n");
scanf("%f", &total);
change(total, &quarters, &dimes, &nickels, &pennies);
printf("TOTAL VALUE ENTERED: $%0.2f",total);
printf("\n%3d quarters\n",quarters);
printf("\n%3d dimes\n",dimes);
printf("\n%3d nickels\n",nickels);
printf("\n%3d pennies\n",pennies);
}
fflush(stdin); /* clear input area so you can pause */
getchar(); /* force the computer to pause until you press a key on the keyboard */
}
int 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 + .005;
return 0;
}