Thread: selection statment

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    19

    selection statment

    try to run a program that if a number is bigger than 0, it state it is positive if not state negative

    my program has a problem on
    Code:
    else
    part

    how can i fix it

    thanks in advance



    Code:
    #include <stdio.h>
    
    int
    main ()
    
    {
        int x;
        printf("enter a value for x");
        scanf("%d", &x);
        
        if (x>0)
        
        {
            printf("%d the number is positive", x);
            
            else
            
                printf("%d the number is negative", x);
            
        }
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hello,
    if you would format your code more elegant and use braces for the body of the is and for the body of else i think you will see the problem then
    Code:
    int t;
    if( t ==5 )
    {
         /* body of the if*/
    }
    else
    {
       /*body of else*/
    }
    If still have problems post back
    Last edited by std10093; 10-20-2012 at 10:42 AM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    thanks
    it works now

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When you have your code in formatted way,the bugs are easier to expose themselves

    Also mind that is good to have this style for main
    Code:
    int main(void)  /*void means no parameters*/
    {
            /*your code*/
           return 0;  /*this declares that everything went well*/
    }

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. On using the goto statment
    By Codegeek892 in forum C Programming
    Replies: 5
    Last Post: 06-24-2012, 05:51 PM
  2. need help with go to statment
    By begginer in forum C Programming
    Replies: 15
    Last Post: 04-06-2011, 10:54 AM
  3. Do While Statment Question?
    By sarajko in forum C Programming
    Replies: 3
    Last Post: 09-24-2010, 02:34 PM
  4. if statment question
    By Carsten in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2002, 05:50 PM
  5. if statment
    By rkjd2 in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2001, 11:48 AM