vendredi 16 juillet 2021

Nested if-else statement in C doesn't work

TASK: "Hotel room costs nothing if you are 60 year old, if you are under 10 year old it costs £5. For everyone else is £30 plus an additional £10 if you weight more than 20 stone.

Write a C programme that reads the customer's age first, then the weight, and then outputs the costs they need to pay."

Here is my code: The nested if-else statement doesn't work, it seems to be stuck after reading the first if statement, it ignores the input options of age >= 10 or age <10, all the outputs are 0.

What is wrong? Please help!

#include <stdio.h>
#include <stdlib.h>

int main (void){

    int age;
    int weight;
    
    scanf ("%d %d", &age, &weight);

    int overweight = weight > 20;
    int underweight = weight <= 20;
 
    if (age = 60) {
        printf ("0\n");
        
    } else if (age >=10  && overweight) {
        printf ("40\n");

    } else if (age >= 10 && underweight) {
        printf ("30\n");
       
    } else if (age < 10) {
        printf ("5\n");
    }    
          return 0;
}

Aucun commentaire:

Enregistrer un commentaire