循环重复scanf和printf

时间:2017-02-17 00:55:11

标签: c arrays printf scanf

我已经在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!

3 个答案:

答案 0 :(得分:5)

您所描述的问题可以追溯到scanf()循环开头的do语句:

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

格式字符串末尾的换行是麻烦。当scanf()遇到格式字符串中的空白字符时,它会匹配输入流中的空白字符,直到遇到非空白字符。问题是当你按下回车键输入你的号码时,这只是另一个空格字符,所以scanf()贪婪地继续等待更多输入,直到非空白字符或{{遇到1}}。当用户此时输入非空白字符时,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:"); ,但仅输入了3天的数据,则计算将访问4high[]数组中未初始化的值。您还需要将此输入循环更改为:

low[]

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

答案 1 :(得分:1)

while (n > 0 || n < 4); =&gt; while (n > 0 && n < 4);

答案 2 :(得分:0)

我能够确定问题的解决方案。我的代码的某些部分的scanf和printf旁边的新行函数导致程序重复某些scanf和printf函数。

scanf("%d\n", &n) => 
printf("%d", &n)

printf ("The average temeprature up to day %d is: %.2f\n", day[n-1], overallAvg); =>  
printf ("The average temeprature up to day %d is: %.2f", day[n-1], overallAvg);