Search code examples
cloopssentinel

How to prevent system from taking the sentinel value as an input?


So, i created a simple program for user to enter temperature and calculate the highest, lowest, average. After user entered the sentinel value to stop the loop, somehow the sentinel value will also be taken as input and messed up the data, here is my code, kindly help me to take a look if u have time, thanks a lot

#include <stdio.h>

int main()
{
    int temperature, highest = 0, lowest = 0, counter = 1, counter2 = 0, total = 0;
    float average;
    
    printf("Enter temperature (-999 to stop) > ");
    scanf("%d", &temperature);
    
    if (temperature == -999) {
    printf("No temperature is captured.");
    return 0;
    }
    
    else if (temperature > 40)
    counter2++;
    
    do {
        printf("Enter temperature (-999 to stop) > ");
        scanf("%d", &temperature);
        
        if (temperature >= highest)
        highest = temperature;
        
        if (temperature <= lowest)
        lowest = temperature;
        
        if (temperature > 40)
        counter2++;
        
        total += temperature;
        counter++;
    } while (temperature != -999);
    
    average = total / counter;
    
    printf("Total days with temperature more than 40'C > %d\n", counter2);
    printf("The lowest temperature  > %d\n", lowest);
    printf("The highest temperature > %d\n", highest);
    printf("Average of temperature  > %.2f\n", average);
}

Solution

  • You include the sentinel (-999) because you add the value before you reach the code that test for the sentinel value. You need to test immediately after you take the input.

    But even if that is fixed, there are more problems.

    You start by setting lowest to zero, so if I input 20 followed by -999, then lowest will still be zero.

    You do not save the first input (except incrementing count2) so your final result will be wrong. Again, if I input 20 followed by -999, then total will be zero (assuming we already fixed the sentinel problem). And if I input 20 40 -999, total will only be 40 and average will be 20 because count is incremented twice.

    Further, you should always check the scanf return value.

    So you need to re-organise your code. For instance like:

    #include <stdio.h>
    
    int main()
    {
        int temperature, highest = 0, lowest = 0, counter = 0, counter2 = 0, total = 0;
        float average;
        
        printf("Enter temperature (-999 to stop) > ");
        if (scanf("%d", &temperature) != 1) exit(1);
        
        if (temperature == -999) {
        printf("No temperature is captured.");
        return 0;
        }
        
        lowest = temperature;
        highest = temperature;
    
        do {
            
            if (temperature >= highest)
            highest = temperature;
            
            if (temperature <= lowest)
            lowest = temperature;
            
            if (temperature > 40)
            counter2++;
            
            total += temperature;
            counter++;
    
            printf("Enter temperature (-999 to stop) > ");
            if (scanf("%d", &temperature) != 1) exit(1);
        } while (temperature != -999);
        
        average = total / counter;
        
        printf("Total days with temperature more than 40'C > %d\n", counter2);
        printf("The lowest temperature  > %d\n", lowest);
        printf("The highest temperature > %d\n", highest);
        printf("Average of temperature  > %.2f\n", average);
    }