Making a change counting program

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-22-2011
Scrogdor
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; }```
• 02-22-2011
Scrogdor
Example of bad entry is if I put in \$0.01 it tells me that I have 1 quarter, 0 dimes, 1 nickel and 2 pennies, which is completely wrong. Anything over \$0.25 is a correct count.
• 02-22-2011
laserlight
Your program would be much easier to read if you formatted it properly and say, moved all those repeated printf calls into a function, e.g.,
Code:

```#include <stdio.h> int change(float total, int *quarters, int *dimes, int *nickels, int *pennies); void print(float total, int quarters, int dimes, int nickels, int pennies); int main(void) {     int quarters, dimes, nickels, pennies;     float total;     total = 1.88;     change(total, &quarters, &dimes, &nickels, &pennies);     print(total, quarters, dimes, nickels, pennies);     total = 0.32;     change(total, &quarters, &dimes, &nickels, &pennies);     print(total, quarters, dimes, nickels, pennies);     printf("\nPlease enter an amount of money: \n");     scanf("%f", &total);     change(total, &quarters, &dimes, &nickels, &pennies);     print(total, quarters, dimes, nickels, pennies);     return 0; } 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; } void print(float total, int quarters, int dimes, int nickels, int 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); }```
As for your problem: you failed to set all those variables to 0. In fact, you should do so at the start of the change function, e.g., assign *quarters = 0;
• 02-22-2011
Scrogdor
The purpose for writing it out is this is a basic class, and as dictated by the professor we are to write everything out at this point in time.

As far as what you said, it didn't make a difference. Like I said, I am not looking for rewrite, but possible reasons it doesn't work under 0.25.
• 02-22-2011
CommonTater
Code:

```    int quarters = 0, dimes = 0, nickels = 0, pennies = 0;     float total = 0.0;```
As laserlight is telling you... you need to initialize variables otherwise you do not know what random values they may hold.
• 02-22-2011
itsme86
Quote:

Originally Posted by Scrogdor
As far as what you said, it didn't make a difference.

I don't believe you. Do it as laserlight said, at the beginning the of the change() function.

Quote:

Originally Posted by CommonTater
Code:

```    int quarters = 0, dimes = 0, nickels = 0, pennies = 0;     float total = 0.0;```
As laserlight is telling you... you need to initialize variables otherwise you do not know what random values they may hold.

That won't work. It needs to be done at either the beginning of the change() function or before every call to the change() function. Otherwise, you'll call change() for the 0.32 value and then when you call it for the user-supplied value the variables will still have the left over values from the last call.
• 02-22-2011
anduril462
By the way, you said main would return an int, so please do so: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]).

Also, fflush(stdin) is wrong. Here are some alternatives if you need to clear the input buffer: Cprogramming.com FAQ > Flush the input buffer.
• 02-23-2011
Scrogdor
Quote:

Originally Posted by itsme86
I don't believe you. Do it as laserlight said, at the beginning the of the change() function.

That won't work. It needs to be done at either the beginning of the change() function or before every call to the change() function. Otherwise, you'll call change() for the 0.32 value and then when you call it for the user-supplied value the variables will still have the left over values from the last call.

If that is the case, and the .32 count affects the user input count, why does everything over 0.25 work as a correct count, but nothing under 0.25 does?
• 02-23-2011
laserlight
Quote:

Originally Posted by Scrogdor
If that is the case, and the .32 count affects the user input count, why does everything over 0.25 work as a correct count, but nothing under 0.25 does?

Examine:
Code:

```    if( total >= 0.25 )         *quarters = (total / 0.25);```
Suppose total is less than 0.25. Then total >= 0.25 evaluates to false, hence *quarters is left with its previous value.

In fact, try this: enter 0 as the total. Notice that the results are exactly the same as for \$0.32.
• 02-23-2011
Scrogdor
Quote:

Originally Posted by laserlight
Examine:
Code:

```    if( total >= 0.25 )         *quarters = (total / 0.25);```
Suppose total is less than 0.25. Then total >= 0.25 evaluates to false, hence *quarters is left with its previous value.

In fact, try this: enter 0 as the total. Notice that the results are exactly the same as for \$0.32.

Now that makes perfect sense. This is my first class of C Programming and knew nothing of it before. So I am learning. Thank you for taking the time to explain, I really appreciate it.
• 02-23-2011
Scrogdor
Thank you for everyones help. I have added the code that was suggested as far as setting everything to 0 before each call. I didn't realize that the third call would build off the second one if no values were entered in to make them change. You guys rock!
• 02-23-2011
CommonTater
Quote:

Originally Posted by laserlight
Examine:
Code:

```    if( total >= 0.25 )         *quarters = (total / 0.25);```

Ok... get out your calculators... lets say the total is 100 ... what is 100 / 0.25?
• 02-23-2011
Scrogdor
Quote:

Originally Posted by anduril462
By the way, you said main would return an int, so please do so: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]).

Also, fflush(stdin) is wrong. Here are some alternatives if you need to clear the input buffer: Cprogramming.com FAQ > Flush the input buffer.

Nothing wrong about it. It does what it is supposed to do so the teacher that is grading our assignment can see the work in action. We do as he directs, not as we want.
• 02-23-2011
itsme86
Quote:

Originally Posted by Scrogdor
Nothing wrong about it.

Wow, when are you going to learn to listen? It already bit you once in this thread and you still continue to argue with the people you're asking for help from. Just because your teacher does it that way doesn't mean there's nothing wrong about it. I for one am done helping you.
• 02-23-2011
CommonTater
Quote:

Originally Posted by Scrogdor
Nothing wrong about it. It does what it is supposed to do so the teacher that is grading our assignment can see the work in action. We do as he directs, not as we want.

Ummmm... Hate to but in here but errors such as fflush(stdin); are common mistakes that, if not corrected, get into real world code and then cause problems. Good programming practices start with "hello world" and build from there. If your instructor tells you otherwise... he's wrong.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last