如何防止系统将标记值作为输入?

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

因此,我创建了一个简单的程序供用户输入温度并计算最高、最低和平均值。用户输入哨兵值停止循环后,不知何故哨兵值也会被作为输入并弄乱数据,这是我的代码,请有时间的人帮我看看,非常感谢

#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);
}

您的代码过于复杂。为什么要在回路外输入第一个温度?第一个输入没有什么特别的。

你想要这样的东西:

#include <stdio.h>

int main()
{
  int temperature, highest = 0, lowest = 0, counter = 1, counter2 = 0, total = 0;
  float average;

  do {
    printf("Enter temperature (-999 to stop) > ");
    scanf("%d", &temperature);

    if (temperature == -999)
      break;

    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);
}

仍然存在一些错误,如果您根本不输入温度,程序将无法正常运行。但我让你自己做。应该不会太难。

您包含标记 (-999),因为您在到达测试标记值的代码之前 添加值 。您需要在接受输入后立即测试

但即使解决了,还有更多问题。

您首先将 lowest 设置为零,因此如果我输入 20,然后输入 -999,那么 lowest 仍将是零。

保存第一个输入(递增count2除外),因此您的最终结果将是错误的。同样,如果我输入 20 后跟 -999,那么 total 将为零(假设我们已经解决了哨兵问题)。如果我输入 20 40 -999,total 将仅为 40,平均值将为 20,因为 count 递增了两次。

此外,您应该始终检查 scanf return 值。

因此您需要 re-organise 您的代码。比如像:

#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);
}