Thread: Quick check

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    13

    Quick check

    Code:
    
    #include <stdio.h>
    
    
    int main()
    {
        float age;
        char sts;
        printf("State your age: ");
        scanf("%f", &age);
        printf("Are you working?\n ");
        scanf("%c", &sts);
    
    
        if ( age > 59 && sts == 'W' )
                printf("Working senior");
        else if ( age > 59 && sts != 'W')
                printf("Retired senior");
        else if ( age < 59 && age > 20 )
                printf("Adult");
        else if ( age < 20 && age > 12 )
                printf("Teen");
        else if ( age < 12 )
                printf("Child");
    }
    What did I do wrong? When I compile and run it, it asks for the age but not the status (sts).

    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    "scanf()" can be tricky if you're not familiar with it. In this code:

    Code:
    scanf("%f", &age);
    ... it reads the user input for a floating point number. When you press 'enter,' it updates the variable with the user input, but it doesn't read the newline character - this remains sitting in the buffer. The newline is picked up by the next "scanf()" automatically, and this makes it appear to "skip" the next "scanf()" call. One way around it is to add a "getchar()" after the first "scanf()." This will eat up that stray newline.

    "scanf()" also returns the number of arguments it successfully read - this can be used to help validate the user input.

    Read up on "scanf()" for more information.
    Last edited by Matticus; 06-25-2012 at 05:34 PM.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Thanks, that did the trick.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    26
    I'm pretty sure that for Age, you should use an int instead of a float variable, right? I guess it doesn't matter a whole lot in this program, but ints are for whole number, and peoples' ages are usually whole numbers.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    what happens when someone is 59, 20 or 12 ?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    FWIW an alternate solution would be to slightly modify the second scanf call with a space in front of the %c specifier.

    Instead of:
    Code:
    scanf("%c", &sts);
    Use this:
    Code:
    scanf(" %c", &sts);
    The difference is subtle but important. The space is a hint to the scanf function that it should throw away leading white-space (space/tab/newline characters). With the newline from the previous scanf call dealt with using such a method, this scanf call will now properly block (wait) for user input as intended by the OP. And it doesn't require an extra line of code (not that one line makes that much of a difference).

    I'd almost go as far as to say that in just about all cases - there may be a few exceptions - any %c specifier in a scanf call should be preceded by a blank space in the format string. Call it 98 or 99% of the time or so.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    775
    As a matter of style, I like to use the following pattern:
    Code:
        if ( age < 12 )
                printf("Child");
        else if ( age < 20 )
                printf("Teen");
        else if ( age < 59 )
                printf("Adult");
        else if ( sts == 'W')
                printf("Working senior");
        else
                printf("Retired senior");
    This pattern reduces range-checking errors, as sparkomemphis brought up, and you only have to modify each age threshold in one place (of course, using #define for each threshold is a good idea too). It's also a good idea to test your program using values at the thresholds as inputs (in this case, 12, 20, and 59).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-06-2012, 07:23 PM
  2. Check to check string for a series of characters
    By robi in forum C Programming
    Replies: 1
    Last Post: 02-28-2012, 05:42 PM
  3. Need a quick check
    By Aliaks in forum C++ Programming
    Replies: 7
    Last Post: 06-05-2009, 04:57 AM
  4. Quick input check question
    By Jozrael in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2009, 07:23 AM
  5. Quick check on reading in + using strings
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 12-08-2008, 10:12 PM