Issues with if statement

This is a discussion on Issues with if statement within the C Programming forums, part of the General Programming Boards category; I am having issues with my if statement and have done everything I can think of to get it to ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    Question Issues with if statement

    I am having issues with my if statement and have done everything I can think of to get it to work but it continues to return the first option no matter what I enter. Could someone please help...

    if (prft1 >= 0)
    { if (prft1 == 0) printf("You have broken even from selling goldfish. \n", prft1 );
    else
    printf("Your profit from selling goldfish is $%.2f. \n", prft1);
    }
    else
    printf("Your loss from selling goldfish is $%.2f. \n", prft1);

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    You might try it like this:

    PHP Code:
    [CODE]

    if(
    prft1==0)
       
    printf("You have broken even from selling goldfish. \n"prft1 );
    else if(
    prft1>0)
       
    printf("Your profit from selling goldfish is $%.2f. \n"prft1);
    else
       
    printf("Your loss from selling goldfish is $%.2f. \n"prft1);

    [/
    CODE

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    [ code ] tags please...

    Have a look at this test version of your code:
    Code:
    #include <stdio.h>
    
    void tester (float prft1);
    
    int main(void)
    {
      float a, b;
      
      tester (0.0);
      tester (1.0);
      tester (-1.0);
      tester (1.1 - 1.1);
      
      a = 1.1;
      b = 2.2;
      tester (a + b - 3.3);
      
      return(0);
    }
    
    void tester (float prft1)
    {
      if (prft1 >= 0)
      {
        if (prft1 == 0)
        {
          printf("You have broken even from selling goldfish. \n", prft1);
        }
        else
        {
          printf("Your profit from selling goldfish is $%.2f. \n", prft1);
        }
      }
      else
      {
        printf("Your loss from selling goldfish is $%.2f. \n", prft1);
      }
    }
    
    /*
    Output:
    
    You have broken even from selling goldfish.
    Your profit from selling goldfish is $1.00.
    Your loss from selling goldfish is $-1.00.
    You have broken even from selling goldfish.
    Your profit from selling goldfish is $0.00.
    
    */
    [edit]
    And yes, a better formatting for the conditional statement would be good (as shown)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    Thumbs down I have

    I have tried both of those and still the same.. The second one that is posted gave me alot of errors... I am abt to pull my hair out...

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post more of your code, together with sample input+output, and any error messages you are getting for it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    How??

    could you tell me how to post it.. I am using a unix system from UCF.

  7. #7
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Copy and paste it?

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Code:
    printf("You have broken even from selling goldfish. \n", prft1);
    Why are you passing prft1 to printf? It will work because of _cdecl, but it's a wrong way to call printf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. Break statement issues, how to return to start of loop?
    By brdiger31 in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2007, 04:29 PM
  3. input/switch statement issues
    By peanut in forum C Programming
    Replies: 5
    Last Post: 10-27-2006, 03:58 PM
  4. switch statement issues...(undeclared identifiers)
    By mero24 in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2005, 08:05 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

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