C 而循环无限循环 EOF 不起作用



我在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 %dn", 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]: https://i.stack.imgur.com/P0307.png

更新:我发现我做错了什么。它很简单。在 while 循环中,while(scanf("%d", &integer) != EOF)不要这样设置它,而是像这样理解(scanf("%d", &integer)) EOF。要在DOS中简单地调用它,请在最后一个输入中使用"Ctrl + Z"。即"数字^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 integersn");
    // 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("nGoodbyen");
    return 0;
}
void findLargestandSmallest(int integer, int *largest, int *smallest)
{
    if (integer < *smallest)
    {
        *smallest = integer;
    }
    if (integer > *largest)
    {
        *largest = integer;
    }
    return;
}

>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 integern");
        exit(1);
    } else {
        /* Do something with integer. */
    }
}

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

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

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

{

scanf("%d",&integer);

....

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

scanf("%d",&check);

}while(check == 1);

针对 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");
}
...

更新:我发现我做错了什么。它很简单。在 while 循环中,while(scanf("%d", &integer) != EOF) 不要这样设置它,而是像这样设置(scanf("%d", &integer)) EOF被理解。要在DOS中简单地调用它,请在最后一个输入中使用"Ctrl + Z"。即"数字^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 integersn");
    // 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("nGoodbyen");
    return 0;
}
void findLargestandSmallest(int integer, int *largest, int *smallest)
{
    if (integer < *smallest)
    {
        *smallest = integer;
    }
    if (integer > *largest)
    {
        *largest = integer;
    }
    return;
}

最新更新