我编写了一个程序,该程序将在scanf之前printf两次,并且将输出应为单个printf的两个。从要求用户输入1到4之间的数字以查看输入的天数的平均温度这一点开始似乎出现了问题。

我不确定是什么原因造成了这种双重输入和输出以及偶尔的延迟。这是我的代码:

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

int main (void) {
    int i;
    int limit;
    int day[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int high[10], low[10];

    printf("---===IPC Temperature Analyzer V2.0===---\n");

    printf("Please enter the number of days between 3 and 10, inclusive: ");
    scanf("%d", &limit);
    while (limit <= 2 || limit >= 11) {
        printf("Invalid entry, please enter a number between 3 and 10, inclusive: ");
        scanf("%d", &limit);
    }

    for (i = 0; i < limit; i++) {
        printf("Day %d - High: ", day[i]);
        scanf("%d", &high[i]);
        printf("Day %d - Low: ", day[i]);
        scanf("%d", &low[i]);
    }

    printf("\nDay Hi Low\n");
    for (i = 0; i < limit; i++) {
        printf("%d   %d    %d\n", day[i], high[i], low[i]);
    }

    int max = 0;
    int min = 0;

    for (i = 0; i < limit; i++) {
        if (high[max] < high[i])
            max = i;
        if (low[min] > low[i])
            min = i;
    }

    printf("\nHighest temperature was: %d on day %d\n", high[max], day[max]);

    printf("Lowest temperature was: %d on day %d\n", low[min], day[min]);

    int n;

    do {
        printf("\nEnter a number between 1 and 4 to see the average temperature "
               "for the entered number of days, enter a negative number to exit:");
        scanf("%d\n", &n);

        while (n > 4) {
            printf("Invalid entry, please enter a number between 1 and 4, inclusive: ");
            scanf("%d", &n);
        }

        while (n < 0) {
            printf("Goodbye!\n");
            exit(0);
        }

        float avgSum = 0.0;
        for (i = 0; i < n; i++) {
            float avgOfDay = (high[i] + low[i]) / 2.0;
            avgSum += avgOfDay;
        }
        float overallAvg = avgSum / n;
        printf("The average temperature up to day %d is: %.2f\n", day[n - 1], overallAvg);

    } while (n > 0 || n < 4);

    return 0;
}


输出示例:

Enter a number between 1 and 4 to see the average temperature for the entered number of days, enter a negative number to exit:5
5
Invalid entry, please enter a number between 1 and 4, inclusive: Invalid entry, please enter a number between 1 and 4, inclusive: 3
The average temperature up to day 3 is: 2.50

Enter a number between 1 and 4 to see the average temperature for the entered number of days, enter a negative number to exit: 2
2
The average temperature up to day 2 is: 2.75

Enter a number between 1 and 4 to see the average temperature for the entered number of days, enter a negative number to exit: -1
The average temperature up to day 2 is: 2.75

Enter a number between 1 and 4 to see the average temperature for the entered number of days, enter a negative number to exit: -1
Goodbye!

最佳答案

您正在描述的问题可以追溯到scanf()循环开始时的do语句:

    scanf("%d\n", &n);


格式字符串末尾的换行符很麻烦。当scanf()在格式字符串中遇到空格字符时,它将与输入流中的空格字符匹配,直到遇到非空格字符。问题是,当您按Enter键输入数字时,这只是另一个空格字符,因此scanf()会贪婪地继续等待更多输入,直到非空格字符或EOF遇到。当用户此时输入非空格字符时,scanf()空格匹配失败,输入的字符保留在输入流中,scanf()最终返回到调用函数,最后返回下一个scanf()拾取刚刚被拒绝的字符。这是您观察到的零星响应的原因。

解决方法很简单。只需从格式字符串的末尾删除\n。通常,格式字符串末尾的空白字符是错误的做法。

代码中还有其他问题。 do循环结束时的测试应为:

while (n > 0 && n < 4);


测试退出值最好使用if语句而不是while循环,并且测试应该使用n < 1而不是n < 0以避免被零错误除法:

if (n < 1) {
    printf("Goodbye!\n");
        exit(0);
}


看来您应该将输入提示更改为:

printf("\nEnter a number between 1 and 3 to see the average temperature for the entered number of days, enter a negative number to exit:");


如果用户在此处选择4,但仅输入了3天的数据,则计算将访问high[]low[]数组中的未初始化值。您还需要将此输入循环更改为:

while (n > 3) {
            printf("Invalid entry, please enter a number between 1 and 3, inclusive: ");
            scanf("%d", &n);
        }


可能还有其他问题,但这应该可以解决问题。

关于c - 循环重复scanf和printf,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42287516/

10-15 02:23