Skipping If Statements?

This is a discussion on Skipping If Statements? within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm running into a little bit of a snag here... I need to make a program that takes ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Question 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);
                              }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    You are using =, the assignment operator, instead of ==, the comparison operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 11:42 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 12:36 PM
  5. having problems with my if statements
    By bajanstar in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 01:39 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21