# stuck on purse program.

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 10-01-2012
rosemary
I would like to make it a loop eventually, but first I was just trying to get the "float" part figured out.
• 10-01-2012
rosemary
It does not loop. It does not add/subtract correctly. I am stuck.
Code:

```  float total=0,t;   int choice=0;                printf("Would You Like to Insert or Remove Coins?\n");         printf("Press 1 for Insert.\n");         printf("Press 2 for Remove.\n");         scanf("%d", &choice); while(!choice) { } if(choice == 1) {         printf( "%d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",                     pennies, nickels, dimes, quarters);         scanf("%d%d%d%d", &p, &n, &d, &q);                 pennies += p;                 nickels += n;                 dimes += d;                 quarters += q;                 total=total+(25*quarters + 10*dimes + 5*nickels + pennies/100);         printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",                     pennies, nickels, dimes, quarters, total); } else {         printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",                       pennies, nickels, dimes, quarters, total);         scanf("%d%d%d%d", &p, &n, &d, &q);                 pennies -= p;                 nickels -= n;                 dimes -= d;                 quarters -= q;                 total=total-(25*quarters + 10*dimes + 5*nickels + pennies/100);         printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f\n",                 pennies, nickels, dimes, quarters, total); } }```
• 10-01-2012
jimblumberg
Let's just look at one section of your code:
Code:

```} if(choice == 1) {     printf( "%d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",             pennies, nickels, dimes, quarters);     scanf("%d%d%d%d", &p, &n, &d, &q);         pennies += p;         nickels += n;         dimes += d;         quarters += q;         total=total+(25*quarters + 10*dimes + 5*nickels + pennies/100);     printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",             pennies, nickels, dimes, quarters, total); }```
In the above code your first printf():
Code:

```    printf( "%d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",             pennies, nickels, dimes, quarters);```
You have 5 format specifiers, yet you only have 4 parameters. Always pass the proper number of parameters to printf(). At this point there is no use in printing total because you haven't computed the total yet.

Next why do you have all the "temporary" variables, p,n,q and d? Just use your pennies, nickles, quarters and dimes in your scanf().

Next when you do math with integers there are no fractions. Therefore in your total calculation you will have problems. The variable pennies is an int, the constant 100 is an int therefore this will yield an int value, no fractions. So if pennies is less than 100 you will get zero. Your total calculation should look like:
Code:

` total=total+(25*quarters) + (10*dimes) + (5*nickels) + pennies);`
Once you have all the values added together and placed in your floating point number you can then do the division:
Code:

`total = total / 100.0`
Jim
• 10-01-2012
rosemary
We posted at same time!
When I enter \$1.41 in change inserted, I get back the total \$141.00 (should be \$1.41).
When I enter \$1.41 in change removed, I get back the total \$141.00 (should be \$0.00).

Why is this? Thanks.

Code:

```#include <stdio.h> #include <math.h> int main(void)   {    int pennies=0, nickels=0, dimes=0, quarters=0, p,n,d,q;   float total=0,t;   int choice=0;        printf("Would You Like to Insert or Remove Coins?\n");     printf("Press 1 for Insert.\n");     printf("Press 2 for Remove.\n");     scanf("%d", &choice); if(choice == 1) {     printf( "%d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",             pennies, nickels, dimes, quarters, total);     scanf("%d%d%d%d", &p, &n, &d, &q);         pennies += p;         nickels += n;         dimes += d;         quarters += q;         total=total+(25*quarters + 10*dimes + 5*nickels + pennies);     printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",             pennies, nickels, dimes, quarters, total); } else {     printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",               pennies, nickels, dimes, quarters, total);     scanf("%d%d%d%d", &p, &n, &d, &q);         pennies -= p;         nickels -= n;         dimes -= d;         quarters -= q;         total=total-(25*quarters + 10*dimes + 5*nickels + pennies);     printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",             pennies, nickels, dimes, quarters, total); } }```
• 10-01-2012
rosemary
Quote:

Originally Posted by jimblumberg
At this point there is no use in printing total because you haven't computed the total yet.

I put that because I thought I would want to show the beginning balance of my purse (if I ever get this into a loop).
• 10-01-2012
jimblumberg
Quote:

When I enter \$1.41 in change inserted, I get back the total \$141.00 (should be \$1.41).
You never do the division. Look closely at my last post.

Quote:

When I enter \$1.41 in change removed, I get back the total \$141.00 (should be \$0.00).
Actually you should get a value of \$-1.41 not zero. Remember total is starting at zero. But look at this calculation:
Code:

`total=total-(25*quarters + 10*dimes + 5*nickels + pennies);`
Also remember that quarters, dimes, nickels, and pennies will be negative. What happens if you subtract a negative value from zero?

Quote:

I put that because I thought I would want to show the beginning balance of my purse (if I ever get this into a loop).
Then you need to add total to the parameters. Remember printf() can really cause problems if you don't pass the correct number of parameters for the number of specifiers.

Jim
• 10-01-2012
rosemary
Yes, we posted at the same time so I hadn't seen your post on total/100 after the point. So, using that, I get the correct total when inserting money.
For removing money, I can get the correct answer if I use total/-100, but is that an incorrect way to do it?
• 10-01-2012
rosemary
Code:

```#include <stdio.h> #include <math.h> int main(void)   {    int pennies=0, nickels=0, dimes=0, quarters=0, p,n,d,q;   float total=0,t;   int choice=0;                printf("Would You Like to Insert or Remove Coins?\n");         printf("Press 1 for Insert.\n");         printf("Press 2 for Remove.\n");         scanf("%d", &choice); if(choice == 1) {         printf( "%d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",                     pennies, nickels, dimes, quarters, total);         scanf("%d%d%d%d", &p, &n, &d, &q);                 pennies += p;                 nickels += n;                 dimes += d;                 quarters += q;                 total=total+(25*quarters) + (10*dimes) + (5*nickels) + pennies;                 total=total/100;         printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",                     pennies, nickels, dimes, quarters, total); } else {         printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",                       pennies, nickels, dimes, quarters, total);         scanf("%d%d%d%d", &p, &n, &d, &q);                 pennies -= p;                 nickels -= n;                 dimes -= d;                 quarters -= q;                 total=total-(25*quarters) - (10*dimes) - (5*nickels) - pennies;                 total=total/-100;         printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2f total\n",                 pennies, nickels, dimes, quarters, total); } }```
• 10-01-2012
jimblumberg
Yes, it really is incorrect. In my opinion you should not use negative numbers with your individual coins, how can you have negative coins? By dealing with only positive coins your calculation would be correct:
Code:

`total = total - ((25 * quarters) + (10 * dimes) + (5 * nickels) + pennies));`
I also suggest that you use parentheses when using division/multiplication and addition/subtraction. They will make your code much easier to follow. Also a little white space also helps readability.

Jim
• 10-01-2012
rosemary
I figured out I needed to change it to +- above and then divide by 100 for the removal. It gives me negative numbers now.
I have negative coins and an awkward layout because I am only in week 4 of of programming :)
I
• 10-01-2012
jimblumberg
Quote:

I have negative coins and an awkward layout because I am only in week 4 of of programming
The amount of time programming really has noting to do with negative coins, it's really just common sense.

Jim
• 10-01-2012
rosemary
i understand that. i just dont know how to do it.
• 10-01-2012
rosemary
if i'm not doing a loop, then there would be no point to the "removed" function if i didn't allow it to go negative. it would always be zero. i'm going to try to figure out a loop. thanks for all the help.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12