Joey Peters
Joey Peters

Reputation: 11

C: how to structure if-else statements

I'm trying to build a weather calculator for a class.

The problem seems to be with my if-else statements. The if's are supposed to print out separate statements about weather conditions based on average temperatures. For some reason, the program always outputs the second statement: "\nThese two weeks were quite hot".

How can I structure my if-else statements correctly?

#include <stdio.h>

int main(void){
    int high_temp[14];
    int warmdays = 0, colddays = 0, i = 0;
    float average = 0.0f, sum = 0.0f;


    printf("\nWeather Analyzer Application by Joey Peters");
    for(i = 0; i < 14; i++) {
        printf("\n\nPlease enter the temperature for day #%d ", i+1);
        scanf("%d", &high_temp[i]);

        sum += high_temp[i];

        if(high_temp[i] > 60){
            warmdays++;
        }

        if(high_temp[i] < 60){
            colddays++;
        }


    }

        average = sum / 14;
        printf("\nThe number of warm days: %d", warmdays);
        printf("\nThe nuber of cold days: %d", colddays);
        printf("\nThe average high temperature: %.2f", average);

        if(average = 100 || average >= 90 ){
            printf("These two weeks were blazing hot");
        }
    else    
        if(average >= 80 || average <= 89){
            printf("\nThese two weeks were quite hot");
        }
    else    

        if(average >= 70 || average <= 79){
            printf("\nThese two weeks hot for Michigan");
        }
    else    

        if(average >= 60 || average <= 69){
            printf("\nThese two weeks were decent for Michigan");
        }
    else    
        if(average >= 50 || average <= 59){
            printf("\nThese two weeks were somewhat cold");
        }
    else
        if(average < 50){
            printf("\nThese two weeks were basically cold");
        }   

    printf("\n\nThank you for using the weather analyzer!");
    return 0;

}       

Upvotes: 0

Views: 136

Answers (1)

Drake
Drake

Reputation: 2703

The line where you have

if(average = 100 || average >= 90 )

You are assigning 100 to x, instead of comparing it. Change it to this:

if(average == 100 || average >= 90 )

Or simplify it further by changing it to

if(average >= 90 )

Since there's no point in checking if average is 100 if you're already checking if it's greater than 90.

Also, if you're checking if average is in between 2 numbers, like 80 and 89 for example, use the && operator:

if(average >= 80 && average <= 89)

Upvotes: 1

Related Questions