-
Skipping If Statements?
Hey guys, I'm running into a little bit of a snag here...
I need to make a program that takes in a monetary value and spits back out the bills and coins that make it up. So I'm using a bunch of if statements to neglect counts of zero and also to make sure that the message back to the user has "and" before the final value. I have a user defined function running the numbers for the program and that works fine. I just seem to be hitting two walls here.
First, in the outer statements, it looks like my program will skip the first if statement after a true one. For example, if it finds a value for the number of tens, it'll skip the number of fives.
Second, the nested if statements are read as false every time, even if they are true.
If anyone would be willing to take the time to look at the code involved (provided below) and perhaps point me in the right direction, it would be greatly appreciated. I don't really want the answer as much as I just want to know what I'm doing wrong.
Thanks for everything.
Chris
Code:
printf("You will get ");
if (twenty > 0)
{
if ((ten = 0) && (five = 0) && (one = 0) && (quarter = 0) &&
(dime = 0) && (nickel = 0) && (penny = 0))
{
printf("%d twenty-dollar bills.\n", twenty);
}
else printf("%d twenty-dollar bills, ", twenty);
}
if (ten > 0)
{
if ((five = 0) && (one = 0) && (quarter = 0) && (dime = 0) &&
(nickel = 0) && (penny = 0))
{
printf("and %d ten-dollar bills.\n", ten);
}
else printf("%d ten-dollar bills, ", ten);
}
if (five > 0)
{
if ((one = 0) && (quarter = 0) && (dime = 0) && (nickel = 0) &&
(penny = 0))
{
printf("and %d five-dollar bills.\n", five);
}
else printf("%d five-dollar bills, ", five);
}
if (one > 0)
{
if ((quarter = 0) && (dime = 0) && (nickel = 0) && (penny = 0))
printf("and %d one-dollar bills.\n", one);
else printf("%d one-dollar bills, ", one);
}
if (quarter > 0)
{
if ((dime = 0) && (nickel = 0) && (penny = 0))
{
printf("and %d quarters.\n", quarter);
}
else printf("%d quarters, ", quarter);
}
if (dime > 0)
{
if ((nickel = 0) && (penny = 0))
{
printf("and %d dimes.\n", dime);
}
else printf("%d dimes, ", dime);
}
if (nickel > 0)
{
if (penny = 0)
{
printf("and %d nickels.\n", nickel);
}
else printf("%d nickels, ", nickel);
}
if (penny > 0)
{
printf("and %d pennies.\n", penny);
}
-
You are using =, the assignment operator, instead of ==, the comparison operator.