Thread: Nested if Statement problem

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    2

    Nested if Statement problem

    Hello there,
    I am new to learning C and have been gradually working my way through the basic if statements, at present i'm stuck making the nesting If statement run the way I want in my program.

    I have attempted to write a program where the user inputs their age and if the user's age is under 18 the program will print "You're too young!". And if the user is over the age of 18 the user can input their gender and a special message will print out determined by the gender they input.


    The problem I have is when the program runs if skips the user option to input their gender and generate the special message.

    Here is the code :

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
         int age;
        char gender;
    
    
        printf("How old are you?\n");
        scanf( "%d",&age );
    
    
         printf("Gender? (m/f)\n");
         scanf( "%c", &gender);
    
    
    
    
        if ( age >= 18 ) {
      
                printf("Welcome");
    
    
                if (gender ==  'm')
                    {
                printf( "guy");
                    }
    
    
            if (gender ==   'f')
             
            {
            printf(  "gal");
            }
                }
                
        if (age < 18)
        {
        printf("Too young");
        }
    
    
        return 0;
    }
    Any advice to where I am going wrong will be appreciated as I have tried hours and hours to find the problem!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    scanf("%d") leaves the newline character in the input stream, and scanf("%c") takes the next available character, which is '\n'. To bypass that problem, tell scanf to ignore whitespace by adding a space before the "%c", like this:
    Code:
    scanf(" %c", &gender);
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    2
    Quote Originally Posted by GReaper View Post
    scanf("%d") leaves the newline character in the input stream, and scanf("%c") takes the next available character, which is '\n'. To bypass that problem, tell scanf to ignore whitespace by adding a space before the "%c", like this:
    Code:
    scanf(" %c", &gender);
    Thank you that sorted it, wow all that trouble over a space!!

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    Just a side note - what if the user does not input m or f? How would
    your program handle that type of problem?
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. several nested "for" conditional statement
    By Joe Cole in forum C Programming
    Replies: 2
    Last Post: 03-17-2016, 08:31 PM
  2. Replacing switch statement with a nested if/else
    By dev123 in forum C Programming
    Replies: 27
    Last Post: 09-06-2010, 12:30 AM
  3. Having Trouble with a nested if else if statement.
    By swicken in forum C Programming
    Replies: 4
    Last Post: 03-20-2010, 09:55 AM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 04:24 PM
  5. Could someone explain nested-if statement to me?
    By shiyu in forum C Programming
    Replies: 13
    Last Post: 02-02-2003, 07:16 PM

Tags for this Thread