I would like to make it a loop eventually, but first I was just trying to get the "float" part figured out.

Printable View

- 10-01-2012rosemary
I would like to make it a loop eventually, but first I was just trying to get the "float" part figured out.

- 10-01-2012rosemary
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-2012jimblumberg
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);

}

Code:`printf( "%d pennies + %d nickels + %d dimes + %d quarters = $%.2f total\n",`

pennies, nickels, dimes, quarters);

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);`

Code:`total = total / 100.0`

- 10-01-2012rosemary
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-2012rosemary
- 10-01-2012jimblumbergQuote:

When I enter $1.41 in change inserted, I get back the total $141.00 (should be $1.41).

Quote:

When I enter $1.41 in change removed, I get back the total $141.00 (should be $0.00).

Code:`total=total-(25*quarters + 10*dimes + 5*nickels + pennies);`

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).

Jim - 10-01-2012rosemary
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-2012rosemaryCode:
`#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-2012jimblumberg
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));`

Jim - 10-01-2012rosemary
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-2012jimblumbergQuote:

I have negative coins and an awkward layout because I am only in week 4 of of programming

Jim - 10-01-2012rosemary
i understand that. i just dont know how to do it.

- 10-01-2012rosemary
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.