c - 循环重复 scanf 和 printf

标签 c arrays printf scanf

我编写了一个程序,它会在 printf 之前执行两次 scanf 操作,并输出两个应该是单个 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/

相关文章:

C 打印十六进制

C - 将 GIF 转换为 JPG

c - 需要释放互斥锁吗?

c - Gtk条目操作

javascript - 从嵌套对象数组中获取具有空值的对象

c++ - 使用 sprintf - 首先查询大小或传递 MAX_PATH 大数组

c - I/O 的触发信号处理程序

javascript - 在单独的对象中查找匹配值

c++ - 编写一个函数的原型(prototype),该函数需要一个正好是 16 个整数的数组

c - printf 返回 c 但出现错误是什么