【问题标题】:Loop repeating scanf and printf循环重复 scanf 和 printf
【发布时间】:2017-07-06 08:45:39
【问题描述】:

我编写了一个程序,它将在 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!

【问题讨论】:

  • 如果你给出预期输出和exact实际输出,你的问题会更清楚。
  • 好的,我已经用正在发生的问题的示例更新了我的帖子。
  • 请注意,拼写一致性对程序员来说是非常可取的——这对于变量名和良好的内部消息至关重要 (temeprature --> temperature)。
  • 由于exit() 不返回,循环while (n &lt; 0) { printf("Goodbye!\n"); exit(0); } 最好写成if 语句:if (n &lt; 0) { printf("Goodbye!\n"); exit(0); }。不过,这不是您当前问题的一部分。
  • 这一行:} while (n &gt; 0 || n &lt; 4); 允许接受任何数字。也许您的意思是:} while (n &gt; 0 &amp;&amp; n &lt; 4); 但是,它只允许值 1,2,3 建议:} while( n&gt;0 &amp;&amp; n &lt;=4)

标签: c arrays printf scanf


【解决方案1】:

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

【讨论】:

  • 纯代码答案没有多大帮助,这并不能解决 OP 报告的问题。对您的提案修复内容进行一些解释会很好,并且通过进一步挖掘 OP 报告的实际问题,您可以将其转化为实际答案。
  • @DavidBowling,感谢您的建议。我是stackoverflow的新手,我的英语不好。
【解决方案2】:

我能够确定我的问题的解决方案。我的代码的某些部分的 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);

【讨论】:

    【解决方案3】:

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

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

    格式字符串末尾的换行符很麻烦。当scanf() 在格式字符串中遇到空白字符时,它会匹配输入流中的空白字符,直到遇到非空白字符。问题是当你按回车键输入你的号码时,这只是另一个空白字符,所以scanf() 贪婪地继续等待更多输入,直到非空白字符或EOF 是遭遇。此时用户输入一个非空白字符,scanf()空白匹配失败,输入的字符留在输入流中,scanf()最终返回调用函数,最后下一个scanf() 拿起刚刚被拒绝的角色。这就是您观察到的零星反应的原因。

    解决这个问题很简单。只需从格式字符串的末尾删除\n。通常情况下,格式字符串末尾的空白字符是错误的。

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

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

    退出值的测试最好使用if 语句而不是while 循环,并且测试应该是n &lt; 1 而不是n &lt; 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);
            }
    

    可能还有其他问题,但这应该可以正常运行。

    【讨论】:

      猜你喜欢
      • 2015-01-30
      • 2016-10-08
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 2013-12-20
      相关资源
      最近更新 更多