I would like to make it a loop eventually, but first I was just trying to get the "float" part figured out.
Printable View
I would like to make it a loop eventually, but first I was just trying to get the "float" part figured out.
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);
}
}
Let's just look at one section of your code:
In the above code your first printf():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);
}
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.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:
Once you have all the values added together and placed in your floating point number you can then do the division:Code:total=total+(25*quarters) + (10*dimes) + (5*nickels) + pennies);
JimCode:total = total / 100.0
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);
}
}
You never do the division. Look closely at my last post.Quote:
When I enter $1.41 in change inserted, I get back the total $141.00 (should be $1.41).
Actually you should get a value of $-1.41 not zero. Remember total is starting at zero. But look at this calculation:Quote:
When I enter $1.41 in change removed, I get back the total $141.00 (should be $0.00).
Also remember that quarters, dimes, nickels, and pennies will be negative. What happens if you subtract a negative value from zero?Code:total=total-(25*quarters + 10*dimes + 5*nickels + pennies);
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.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
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?
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);
}
}
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:
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.Code:total = total - ((25 * quarters) + (10 * dimes) + (5 * nickels) + pennies));
Jim
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
The amount of time programming really has noting to do with negative coins, it's really just common sense.Quote:
I have negative coins and an awkward layout because I am only in week 4 of of programming
Jim
i understand that. i just dont know how to do it.
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.