Nested if statement in C

Viewed 395

My code need to execute only one case path from 3x2 table which is; for Male 140 to 160 cm: short, 161 to 180 cm: medium; 181 to 199 cm: tall and for Female 120 to 140 cm: short, 141 to 165 cm: medium and 166 to 180 cm: tall.

For example, if i enter 153 cm woman, output is need to be only medium. But now my code gives output if i enter 153 cm woman, medium & tall together at the end.

How can i edit this code so far to execute for only one case for two combinations, age and length, and one of the three option for length; for example if i enter 153 cm woman, it will need say only medium.

#include <stdio.h>

int main()
{

    int length;
    char gender;

    printf("enter gender: ");
    scanf("%c", &gender);

    printf("enter length: ");
    scanf("%d", &length);

    if (gender == 'M' || 'm') {

        if (length <= 140 && length <= 160 ) {

            printf ("short");
        }

        if (length <= 161 && length <= 180 ) {

            printf ("medium");

        }

        if (length <= 181 && length <= 199 ) {

            printf ("tall");
        }

        if (gender == 'W' || gender == 'w') {

            if (length <= 120 || length <=140) {

                printf("short");

                if (length <=141 || length <=165) {

                    printf ("medium");

                    if (length <=166 || length <=180) {

                        printf ("tall");}

                        else {

                            printf("error");
                        }
                    }
                }
            }
        }



    return 0;
}
3 Answers

For below lines, it has to be length >= 161. Pls check similar lines for this.

   if (length <= 161 && length <= 180 ) {

        printf ("medium");

    }

kadir you have done a number of mistakes , no problem . Practice makes a man perfect ...! Compare your code and your code that i changed .. and realize the mistakes . Explanation is given as comments in code .

#include<stdio.h>

int main()
{

int length;
char gender;

printf("enter gender: ");
scanf("%c", &gender);

printf("enter length: ");
scanf("%d", &length);

if (gender == 'M' ||gender == 'm') { //comparison should be done in both sides of or

    if (length >= 140 && length <= 160 ) {

        printf ("short");
    }

    else if (length >= 161 && length <= 180 ) {

        printf ("medium");

    }

   else if (length >= 181 && length <= 199 ) {

        printf ("tall");
    }
}
 else if (gender == 'W' || gender == 'w') {
        if (length >= 120 && length <=140) {

            printf("short");
        }
          else  if (length >=141 && length <=165) { // & should be used.

                printf ("medium");
            }
              else  if (length >=166 && length <=180) {

                    printf ("tall");}

                    else {   /* if you are using else simply , it corresponds to the last if not all the if's in front so try using if else .*/

                        printf("error");
                    }
                }



return 0;
 }

Good Luck

You need to compare length in an exclusive range. Here is the code:

#include <stdio.h>

int main()
{

int length;
char gender;

printf("enter gender: ");
scanf("%c", &gender);

printf("enter length: ");
scanf("%d", &length);

if (gender == 'M' || gender == 'm'){
    if (length <= 160 )
        printf ("short\n");
    if (161 <= length && length <= 180)
        printf ("medium\n");
    if (181 <= length && length <= 199)
        printf ("tall\n");

}

if (gender == 'W' || gender == 'w'){
    if (length <= 140) 
        printf ("short\n");
    if (141 <= length && length <= 165)
        printf ("medium\n");
    if (166 <= length && length <= 180)
        printf ("tall\n");
        if(length>=180)
        printf("error \n");
 }
 else if(gender != 'm'|| gender!='M'|| gender!='w'||gender!='W')
 printf("error");
 return 0;

}
Related