C while循环无限循环EOF不工作

时间:2014-03-05 16:45:47

标签: c loops while-loop eof infinite

我在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", &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 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;
}

4 个答案:

答案 0 :(得分:1)

scanf返回成功转换的元素数。如果它无法转换,则返回0. EOF仅返回文件结尾(在Un​​ix上为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. */
    }
}

答案 1 :(得分:0)

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

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

所以我建议你使用do while loop

DO

{

的scanf( “%d”,&安培;整数);

...

... printf(“输入1继续”);

的scanf( “%d”,&安培;检查);

} while(check == 1);

答案 2 :(得分:0)

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

答案 3 :(得分:0)

更新:我发现我做错了什么。这很简单。在while循环while(scanf(“%d”,&amp;整数)!= EOF)不要像那样设置它(scanf(“%d”,&amp; 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 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;
}