【问题标题】:C while loop infinite loop EOF not workingC while循环无限循环EOF不起作用
【发布时间】:2014-03-05 16:45:46
【问题描述】:

我在 while 循环中遇到了 EOF 的问题。当输入EOF 时,它似乎并没有简单地结束,而是这样做......

我怎样才能修复它并让 while 循环停止并继续。谢谢。

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <limits.h>

    void findLargestandSmallest(int integer, int* largest, int* smallest, int* average, int count);

    int main(void)
    {
        //Local Declaration
        int integer;
        int largest;
        int smallest;
        int average;
        int count; // number count

        //Starting statemnets
        smallest = INT_MAX;
        largest = INT_MIN;
        count = 0;

        // Starting prompts
        printf("\nHello this program will take in intagers and print");
        printf("\nout the largest, smallest and avarage of integers");
        printf("\nenterd int.");
        printf("\nPlease enter in a integer ");

        while (scanf("%d", &integer) != EOF)
        {
            if (integer != EOF)
            {
                count++;
                findLargestandSmallest(integer, &largest, &smallest, &average, count);
            }
            else
            {
                printf("\n \n");
            }
        }

        printf("\nThe largest number entered is %d and the smallest", largest);
        printf("\nwas %d and the average of all the numbers is %d\n", smallest, average);
        return 0;
    }

    void findLargestandSmallest(int integer, int *largest, int *smallest, int *average, int count)
    {
        int x; // just a holder variable 

        // finds average
        x = 0;
        x += integer;
        *average = (x / count);

        // Finds smallest and largest
        if (integer <= *smallest)
        {
            *smallest = integer;
        }
        if (integer >= *largest)
        {
            *largest = integer;
        }
        printf("Enter another integer or <EOF> to quit ");
        return;
    }


  [1]: http://i.stack.imgur.com/P0307.png

更新:我发现我做错了什么。这很简单。在while循环中while(scanf("%d", &amp;integer) != EOF)不要这样设置,但是像这样(scanf("%d", &amp;integer))EOF是可以理解的。要在 DOS 中简单地调用它,请在最后一次输入时使用“Ctrl+Z”。即“number^Z”是使用“Ctrl+Z”后的外观。对于遇到此问题的其他人来说,这里是解决此问题的更好且有效的代码。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <limits.h>

void findLargestandSmallest(int integer, int* largest, int* smallest);

int main(void)
{
    //Local Declaration
    int integer;
    int largest;
    int smallest;
    int average;
    int sum;
    int count;

    //Starting statemnets
    smallest = INT_MAX;
    largest = INT_MIN;
    count = 0;
    sum = 0;

    // Starting prompts
    printf("\n--------------------------------------------------------");
    printf("\n-  Hello, this program will take in intagers and print -");
    printf("\n-  out the largest, smallest, and avarage  of the      -");
    printf("\n-  integers enterd.                                    -");
    printf("\n-  NOTE: To quit: use \"Ctrl+Z\" on the last integer     -");
    printf("\n-  you enter i.e \"number^z\"                        -");
    printf("\n--------------------------------------------------------\n");
    printf("\nEnter integers\n");

    // Finds largest and smallest number
    while (scanf("%d", &integer))
    {
        sum += integer;
        count++;
        findLargestandSmallest(integer, &largest, &smallest);
    }

    // Finds average
    count--;
    average = (sum / count);

    // End prompts
    printf("\n--------------------------------------------------------");
    printf("\nThe largest number entered was %d, the smallest", largest);
    printf("\nwas %d, and the average of all the numbers is %d.", smallest, average);
    printf("\n--------------------------------------------------------");
    printf("\nGoodbye\n");
    return 0;
}

void findLargestandSmallest(int integer, int *largest, int *smallest)
{
    if (integer < *smallest)
    {
        *smallest = integer;
    }
    if (integer > *largest)
    {
        *largest = integer;
    }
    return;
}

【问题讨论】:

  • printf("Enter another integer or &lt;EOF&gt; to quit ");应该在while循环的末尾,而不是findLargestandSmallest函数的末尾。
  • 你是怎么引起EOF的?通过输入“EOF”、Ctrl-D 或其他内容?

标签: c loops while-loop eof infinite


【解决方案1】:

scanf 返回成功转换的元素数。如果它不能转换任何内容,则返回 0。EOF 仅在文件结束时返回(在 Unix 上为 Control-D)。

所以你应该修改你的程序来保存scanf的返回值,然后分别测试0和EOF

integer 变量与EOF 进行比较也是没有意义的,因为您可能只知道EOF 是一个负整数。阅读scanf 手册页,了解它的作用以及它在何时何地返回什么。这将解决难题。 :-)

好的,还有一些提示。你能理解这个吗?

for (;;) {
    int successfully_converted = scanf("%d", &integer);

    if (successfully_converted == EOF) {
        /* Do something when the user is tired of typing. */
        puts("Thank you for an enjoyable game.\n");
        exit(0);
    } else if (successfully_converted == 0) {
        puts("This didn't look like an integer\n");
        exit(1);
    } else {
        /* Do something with integer. */
    }
}

【讨论】:

  • 特别是,您似乎没有做任何事情来跳过数字之间存在的空格 - 所以您扫描第一个数字,然后每个 scanf() 之后返回 0,因为它不能当下一个字符是空格或换行时扫描一个数字,并且 0 永远不会匹配EOF,所以你有一个无限循环......
  • 好吧,它不完全理解如何做到这一点......我想在 scnaf 中输入的值之一是 0。我仍然掌握计算机编程的窍门。你能试着详细解释一下吗?
  • @twalberg scanf("%d"... 跳过前导空格。跳过空格不是 OP 的问题。
  • @chux 是的,你是对的,现在我再想一想。但是,输入中任何不能作为整数扫描的非空白字符都会导致这样的问题......
【解决方案2】:

您没有将整数值与 EOF 进行比较 .您正在将 scanf 结果与 EOF 进行比较。 这里每次输入 1 个值,scanf 结果为 1。

所以 evrrytime while 循环条件失败并生成无限循环。 另外,如果您是EOF,那么您将输入什么字符来结束循环??? 所以不应该使用EOF。

所以我建议你使用 do while 循环

{

scanf("%d",&integer);

....

... printf("输入 1 继续");

scanf("%d",&check);

}while(check == 1);

【讨论】:

  • 我有一个疑问。你为什么用EOF???你会输入什么字符来结束循环?所以这可能是一个原因......
  • 已根据上述 cmets 编辑了我的解决方案.. 请检查
【解决方案3】:

针对 EOF、0 和 1 测试 scanf() 的结果。

int cnt;
while ((cnt = scanf("%d", &integer)) == 1) {
  count++;
  findLargestandSmallest(integer, &largest, &smallest, &average, count);
}
if (cnt == 0) {
  printf("Something other than an integer was entered.\n");
}
else if (cnt == EOF) {
  printf("EOF or I/O Error occurred.\n");
}
// Add the following for debug if desired
else {
  printf("Should _never get here!\n");
}
...

【讨论】:

    【解决方案4】:

    更新:我发现我做错了什么。这很简单。在 while 循环中 while(scanf("%d", &integer) != EOF) 不要像这样设置它,而是像这样 (scanf("%d", &integer)) EOF 被理解。要在 DOS 中简单地调用它,请在最后一次输入时使用“Ctrl+Z”。即“number^Z”是使用“Ctrl+Z”后的外观。对于遇到此问题的其他人来说,这里是解决此问题的更好且有效的代码。

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <limits.h>
    
    void findLargestandSmallest(int integer, int* largest, int* smallest);
    
    int main(void)
    {
        //Local Declaration
        int integer;
        int largest;
        int smallest;
        int average;
        int sum;
        int count;
    
        //Starting statemnets
        smallest = INT_MAX;
        largest = INT_MIN;
        count = 0;
        sum = 0;
    
        // Starting prompts
        printf("\n--------------------------------------------------------");
        printf("\n-  Hello, this program will take in intagers and print -");
        printf("\n-  out the largest, smallest, and avarage  of the      -");
        printf("\n-  integers enterd.                                    -");
        printf("\n-  NOTE: To quit: use \"Ctrl+Z\" on the last integer     -");
        printf("\n-  you enter i.e \"number^z\"                        -");
        printf("\n--------------------------------------------------------\n");
        printf("\nEnter integers\n");
    
        // Finds largest and smallest number
        while (scanf("%d", &integer))
        {
            sum += integer;
            count++;
            findLargestandSmallest(integer, &largest, &smallest);
        }
    
        // Finds average
        count--;
        average = (sum / count);
    
        // End prompts
        printf("\n--------------------------------------------------------");
        printf("\nThe largest number entered was %d, the smallest", largest);
        printf("\nwas %d, and the average of all the numbers is %d.", smallest, average);
        printf("\n--------------------------------------------------------");
        printf("\nGoodbye\n");
        return 0;
    }
    
    void findLargestandSmallest(int integer, int *largest, int *smallest)
    {
        if (integer < *smallest)
        {
            *smallest = integer;
        }
        if (integer > *largest)
        {
            *largest = integer;
        }
        return;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-30
      • 2013-02-22
      • 1970-01-01
      • 2016-08-27
      • 2015-04-29
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多