Thread: Nested if statement not working

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    47

    Exclamation Nested if statement not working

    hello friends
    i am new to this forum

    i am having some problem with nested if statement

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main()
    {
         int age;
         char gen;
    printf("Enter your age :  \n");
    scanf("%d",&age);
    
    
    printf("Whats your gender m/f :  \n");
    scanf(" %c ",&gen);
    
    
    
    
    if (age >= 18){
    
    
        printf("enter this website");
    
    
        if (gen == 'm'){
        printf("dude");
    
    
        }
    
    
    
    
        }
    
    
    
    
    
    
        return 0;
    }
    when i run this code after enter age ,enter gender nothing shows on screen and program ends there

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Just before the return 0, do this
    Code:
     putchar();
    It will catch your code before closing

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    47
    hello it shows error

    Nested if statement not working-err1-jpg

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    He probably meant getchar(), not putchar()

    Also, the trailing space after %c in the second scanf is going to mess you about as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Sorry, I did mean "getchar"

  6. #6
    Registered User
    Join Date
    May 2019
    Posts
    47
    hello still no luck

    Nested if statement not working-noluck-jpg

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Try putting a newline at the end "dude", or adding...
    Code:
    fflush(stdout);
    after your last printf to flush the output buffer

  8. #8
    Registered User
    Join Date
    May 2019
    Posts
    47
    Quote Originally Posted by Click_here View Post
    Try putting a newline at the end "dude", or adding...
    Code:
    fflush(stdout);
    after your last printf to flush the output buffer

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main()
    {
         int age;
         char gen;
    printf("Enter your age :  \n");
    scanf("%d",&age);
    
    
    printf("Whats your gender m/f :  \n");
    scanf(" %c ",&gen);
    
    
    
    
    if (age >= 18){
    
    
        printf("enter this website");
    
    
        if (gen == 'm'){
        printf("dude");
    
    
        }
    
    
    
    
        }
    
    
     getchar();
     fflush(stdout);
        return 0;
    }

    you meant this?

    this also doesn't print anything after age and gender is entered

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change this:
    Code:
    scanf(" %c ",&gen);
    to this:
    Code:
    scanf(" %c", &gen);
    Notice I removed the trailing space in the format string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2019
    Posts
    47
    Quote Originally Posted by laserlight View Post
    Change this:
    Code:
    scanf(" %c ",&gen);
    to this:
    Code:
    scanf(" %c", &gen);
    Notice I removed the trailing space in the format string.
    oh it worked! thanks...

    now it works without these lines
    Code:
     getchar();
     fflush(stdout);
    but does c have space issues ?
    Last edited by sash_007; 05-09-2019 at 07:18 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The trailing space in the format string matches whitespace, so the issue is that you then need to have say, a trailing space in your input for it to be matched, otherwise your program would keep waiting for input to match. This is why it looked like nothing happened: your program was waiting for more input. Note that Salem mentioned this in post #4.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop with nested switch statement help
    By Passwaters in forum C# Programming
    Replies: 4
    Last Post: 09-19-2018, 11:11 PM
  2. Nested SWITCH statement is not working for unknown reason
    By carloswm85 in forum C Programming
    Replies: 2
    Last Post: 02-09-2018, 09:54 AM
  3. Nested if Statement problem
    By Cochise in forum C Programming
    Replies: 3
    Last Post: 08-03-2016, 02:19 AM
  4. 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
  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