Question

I am having problem with EOF in my while loop. It does not seem to simply end when EOF is entered but rather does this...

How can I fix it and have the while loop stop and move on. Thanks.

    #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

UPDATE: I found out what I was doing wrong. Its simple. In the while loop while(scanf("%d", &integer) != EOF) don't set it like that but like this (scanf("%d", &integer)) EOF is understood. To simply call it in DOS use use "Ctrl+Z" on your last input. i.e "number^Z" is how it will look after using "Ctrl+Z" Also here is the better and working code for this problem for anyone else that runs into this.

#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;
}
Was it helpful?

Solution 4

UPDATE: I found out what I was doing wrong. Its simple. In the while loop while(scanf("%d", &integer) != EOF) don't set it like that but like this (scanf("%d", &integer)) EOF is understood. To simply call it in DOS use use "Ctrl+Z" on your last input. i.e "number^Z" is how it will look after using "Ctrl+Z" Also here is the better and working code for this problem for anyone else that runs into this.

#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;
}

OTHER TIPS

scanf returns the number of elements successfully converted. If it can't convert any, it returns 0. EOF is only returned for end-of-file (on Unix a Control-D).

So you should modify your program to save the return value from scanf and then test it for 0 and EOF separately.

It is also pointless to compare the integer variable with EOF, since all you can possibly know about EOF is that it is a negative integer. Read the scanf manual page and understand what it does and what it returns when and where. That'll solve the puzzle. :-)

Alright, some more hints. Can you make sense of this?

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. */
    }
}

You are not comparing the integer value with EOF . You are comparing the scanf result with EOF.. Here as you enter 1 value each time, scanf result will be 1.

So evrrytime the while loop condition fails and infinite loop is generated. Also if you EOF then what character would you enter to end the loop??? So EOF should not be used.

So I would suggest you to use do while loop

do

{

scanf("%d",&integer);

....

... printf("enter 1 to continue");

scanf("%d",&check);

}while(check == 1);

Test the result of scanf() against EOF, 0, and 1.

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");
}
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top