Like Tree1Likes
  • 1 Post By std10093

Scanf inside IF statement not working

This is a discussion on Scanf inside IF statement not working within the C Programming forums, part of the General Programming Boards category; This is the code, after i enter age:60+ , it goes straight to the output and doesnt execute th scanf ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Scanf inside IF statement not working

    This is the code, after i enter age:60+ , it goes straight to the output and doesnt execute th scanf inside, thanks for the help

    Code:
    #include<stdio.h>
    int main(void)
    {
        int age;
        char sts;
        printf("Enter the age: \n");
        scanf("%d", &age);
        
        if(age>59)
            {
            printf("Enter W for working or R for retired: \n");
            scanf("%c", &sts);
                if(sts=='W')
                    printf("Working senior\n");
                else
                    printf("Retired Senior\n");
            }
        else if(age>20)
            printf("Adult \n");
        else if(age>12)
            printf("Teen \n");
        else
            printf("Child \n");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    Quote Originally Posted by ace88 View Post
    This is the code, after i enter age:60+ , it goes straight to the output and doesnt execute th scanf inside, thanks for the help

    Code:
    #include<stdio.h>
    int main(void)
    {
        int age;
        char sts;
        printf("Enter the age: \n");
        scanf("%d", &age);
        
        if(age>59)
            {
            printf("Enter W for working or R for retired: \n");
            scanf(" %c", &sts);
                if(sts=='W')
                    printf("Working senior\n");
                else
                    printf("Retired Senior\n");
            }
        else if(age>20)
            printf("Adult \n");
        else if(age>12)
            printf("Teen \n");
        else
            printf("Child \n");
        return 0;
    }
    now it should work

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What root wants to say is :
    When you enter the age think of the events happening in your keyboard.You tupe 61 for example and then you hit enter.So the buffer where the data from stdin is stored to , in order your program to handle this data contains the number and the \n (newline character).
    So you read the number with
    Code:
    scanf("%d", &age);
    And then you have another scanf which is going to consider as input given the newline that was left by your first input .
    So you write
    Code:
    scanf(" %c", &sts);
    , so that scanf skips the newline.(What you have to do is leave a space before %c)
    root likes this.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    I see, thanks to both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf dont work inside in a while
    By WolfBend in forum C Programming
    Replies: 3
    Last Post: 03-28-2010, 07:54 PM
  2. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 02:18 PM
  3. Replies: 5
    Last Post: 06-23-2009, 03:51 AM
  4. switch statement inside a do while
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:33 PM
  5. cin.get() inside switch statement
    By timberwolf5480 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 12:26 AM

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