Thread: Using more than one if's sub routine in one program

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    30

    Question Using more than one if's sub routine in one program

    Can we use more than one 'if' subroutine in one program.
    I am trying to implement this but if I run the two subroutines at the same time the lower subroutine directly shows "invalid grade". But if I run one at a time then they work perfectly fine.
    Why is this happening? Does the compiler think that its the same "if" routine. If so how do I segregate it???
    Code:
            printf("Enter a score:");
        scanf("%d",&gr);
        if(gr>=80 && gr<=100)
        printf("A Grade\n");
        if (gr>=60 && gr<80)
        printf("B Grade\n");
        if (gr>=40 && gr<60)
        printf("C Grade\n");
        if (gr>=20 && gr<40)
        printf("D Grade\n");
        if (gr<20)
        printf("E Grade\n");
        if(gr<0 && gr>100) 
        printf("invalid\n\n");
        
    
    
        printf("Enter a grade:\n");
        scanf("%c",&gre);
        if(gre=='A')
        printf("Range: 80 to 100 \n");
        if (gre=='B')
        printf("Range: 60 to 79 \n");
        if (gre=='C')
        printf("Range: 40 to 59\n");
        if (gre=='D')
        printf("Range: 20 to 39\n");
        if (gre=='E')
        printf("Range: 0 to 19\n");
        if (gre!='A'&& gre!='B'&& gre!='C'&& gre!='D'&& gre!='E') 
        printf("invalid grade\n\n");
    Last edited by shan2014; 03-20-2014 at 07:37 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    It is the "if" statement NOT "if" subroutine.

    This is the line of code with the problem.
    Code:
    scanf("%c",&gre);
    Change to
    Code:
    scanf(" %c",&gre);
    The space before the "%" tells the scanf to skip over whitespace.
    This includes the newline.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    you need to clear the buffer after the first scanf(), the FAQ page has a nice page explaining this

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by stahta01 View Post
    It is the "if" statement NOT "if" subroutine.

    This is the line of code with the problem.
    Code:
    scanf("%c",&gre);
    Change to
    Code:
    scanf(" %c",&gre);
    The space before the "%" tells the scanf to skip over whitespace.
    This includes the newline.

    Tim S.
    Huh! Can a space actually help the compiler to understand that it is a separate "if" statement

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by africanwizz View Post
    you need to clear the buffer after the first scanf(), the FAQ page has a nice page explaining this
    Okay! I will check it out!

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by shan2014 View Post
    Huh! Can a space actually help the compiler to understand that it is a separate "if" statement
    No, but that is NOT the problem in your code.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by stahta01 View Post
    No, but that is NOT the problem in your code.

    Tim S.
    And it works like charm!
    Thanks a lot !!!!

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Consider using else if to simplify this -- which you should try to do whenever you have mutually exclusive cases (i.e. a grade can't be both an A and a B at the same time). Start at the highest (invalid score, > 100) and work down:
    Code:
    #define MAX_SCORE 100
    #define A_MIN_SCORE 80
    #define B_MIN_SCORE 60
    ...
    if (grade > MAX_SCORE)
        print "invalid"
    else if (grade >= A_MIN_SCORE)
        print "A"
    else if (grade >= B_MIN_SCORE)
    ...
    Note a few things:

    • I used named constants to make your code easier to read, modify and maintain.
    • The else part of the else if ensures that all previous conditions checked are false. Thus if we work down (or up) in order, we only have to check the lower (or upper) limit of a range.
    • Technically this is a bit quicker too, as it will stop checking the if/else if conditions once it matches one. Your version checks every if, even

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by anduril462 View Post
    Consider using else if to simplify this -- which you should try to do whenever you have mutually exclusive cases (i.e. a grade can't be both an A and a B at the same time). Start at the highest (invalid score, > 100) and work down:
    Code:
    #define MAX_SCORE 100
    #define A_MIN_SCORE 80
    #define B_MIN_SCORE 60
    ...
    if (grade > MAX_SCORE)
        print "invalid"
    else if (grade >= A_MIN_SCORE)
        print "A"
    else if (grade >= B_MIN_SCORE)
    ...
    Note a few things:

    • I used named constants to make your code easier to read, modify and maintain.
    • The else part of the else if ensures that all previous conditions checked are false. Thus if we work down (or up) in order, we only have to check the lower (or upper) limit of a range.
    • Technically this is a bit quicker too, as it will stop checking the if/else if conditions once it matches one. Your version checks every if, even
    Wow. Thanks a lot! Really simplifies the code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this an example of a co-routine?
    By c_weed in forum Tech Board
    Replies: 1
    Last Post: 06-20-2012, 11:36 AM
  2. Name for this procedure/routine?
    By Striph in forum C Programming
    Replies: 2
    Last Post: 06-23-2009, 09:09 AM
  3. Help with my XOR routine..
    By Neo1 in forum C++ Programming
    Replies: 9
    Last Post: 10-04-2007, 03:30 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. C Routine for a Date
    By kgiles in forum C Programming
    Replies: 3
    Last Post: 12-28-2001, 05:10 PM

Tags for this Thread