plathys
plathys

Reputation: 9

Why my codes 'if statement' doesn't work in C?

I wrote a program that you input your qualities and the program decides that if you are suitable for being astronaut.However, it works when taking the inputs and doesn't continue to the 'if statement' part.

CODE :

#include <stdio.h>

int
main(void)
{
    int opt_min;
    int opt_max;
    int age_min;
    int age_max;
    int age;
    double weight;
    char smoking;

    printf("Enter the minimum weight an astronaut can be>");
    scanf("%d", &opt_min);

    printf("Enter the maximum weight an astronaut can be>");
    scanf("%d", &opt_max);

    printf("Enter the minimum age an astronaut can be>");
    scanf("%d", &age_min);

    printf("Enter the maximum age an astronaut can be>");
    scanf("%d", &age_max);

    printf("Enter your weight>");
    scanf("%lf", &weight);

    printf("Enter your age>");
    scanf("%d", &age);

    printf("Enter the state of smoking\n(if a smoker press 'S' if not press another letter and press return)>");
    scanf(" %c", &smoking);

    if (weight >= opt_min && weight <= opt_max) {
        if (age >= age_min && age <= age_max) {
            if (smoking != 'S' || smoking != 's') {
                printf("You can be an astronaut!");
            }
        }
    }
    else {
        printf("Sorry! Your qualities are not enough to be an astronout.");
    }

    return (0);
}

There are no errors and I don't think there is syntax error either. It takes the inputs and then jumps to the return(0) part. Please help me with this.

COMPLIER:

Enter the minimum weight an astronaut can be>50
Enter the maximum weight an astronaut can be>150
Enter the minimum age an astronaut can be>20
Enter the maximum age an astronaut can be>50
Enter your weight>55
Enter your age>19
Enter the state of smoking
(if a smoker press 'S' if not press another letter and press return)>j
Program ended with exit code: 0

Upvotes: -1

Views: 73

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

You are correctly using the logical AND operator in the if statements except the last one

if (smoking != 'S' || smoking != 's') {

You have to write

if (smoking != 'S' && smoking != 's') {

As for your input

Enter your age>19

then 19 is not in the accepted range of ages.

And the else part belongs to the outer most if statement that gets the control

if (weight >= opt_min && weight <= opt_max) {
    //...
}
else {
//...

So the else part of the if statement is skipped.

You could introduce one more variable that would store the result of the conditions as for example

int valid = (weight >= opt_min && weight <= opt_max) &&
            (age >= age_min && age <= age_max) &&
            (smoking != 'S' && smoking != 's');

if ( valid )
{
    puts("You can be an astronaut!");
}
else 
{
    puts("Sorry! Your qualities are not enough to be an astronout.");
}

Upvotes: 1

Craig Estey
Craig Estey

Reputation: 33631

If the weight is within range but age or smoking is not, you will get no output.

Change:

if (weight >= opt_min && weight <= opt_max) {
    if (age >= age_min && age <= age_max) {
        if (smoking != 'S' || smoking != 's') {
            printf("You can be an astronaut!");
        }
    }
}
else {
    printf("Sorry! Your qualities are not enough to be an astronout.");
}

Into:

if ((weight >= opt_min) && (weight <= opt_max) &&
    (age >= age_min) && (age <= age_max) &&
    (smoking != 'S') && (smoking != 's')) {
        printf("You can be an astronaut!");
}
else {
    printf("Sorry! Your qualities are not enough to be an astronout.");
}

Upvotes: 2

Related Questions