![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 1
| 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);
}
|
| CBecker5000 is offline | |
| | #2 |
| Registered User Join Date: Oct 2006
Posts: 103
| You are using =, the assignment operator, instead of ==, the comparison operator. |
| MWAAAHAAA is offline | |
![]() |
| Tags |
| if statements, skip |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unknown memory leak with linked lists... | RaDeuX | C Programming | 6 | 12-07-2008 04:09 AM |
| newbie question - if statements without conditions | c_h | C++ Programming | 2 | 07-18-2008 10:42 AM |
| Interpreter.c | moussa | C Programming | 4 | 05-28-2008 05:59 PM |
| Efficiency of case statements | Yasir_Malik | C Programming | 26 | 05-23-2006 11:36 AM |
| having problems with my if statements | bajanstar | C Programming | 3 | 03-05-2005 01:39 PM |