Thread: EOF and Functions

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    3

    EOF and Functions

    Hello.
    I am new to this website. I just had a quick question regarding the matter of EOF.

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    
    int average (int smallest);
    
    
    int main (void)
    {
      //  int n;
        int small;
       // int largest;
    
    
        average (small);
    
    
        printf("In main, smallest is: %d\n", small);
    
    
        return 0;
    }
    
    
    int average (int smallest)
    {
        int n;
        int largest;
    
    
        smallest = INT_MAX;
        largest = INT_MIN;
    
    
        printf("Please enter positive integers (EOF to stop): \n");
        while(scanf("%d", &n) != EOF)
        {
            if (n < smallest)
            smallest = n;
    
    
            if (n > largest)
            largest = n;
    
    
            if (n < 0)
                {
                    printf("\nERROR!");
                    printf("\nYou have either entered a negative integer \nor the integer you entered is too long.\n");
                    exit(0);
                }
        }
    
    
        printf("Smallest number is: %d\n", smallest);
        printf("Largest number is: %d\n", largest);
        return average;
    }
    When I execute the program, the "main" function does not print "smalest" number, but rather prints some weird numbers

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're not calling average correctly. It should be "small = average();" So average should have the declaration "int average(void);"

    At the end of "average" you say "return average" which doesn't make sense. Presumably you want "return smallest". And smallest should be defined inside average.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    scanf - C++ Reference

    Read the "return value" section at least.

    Edit: While what you posted might work most people check for the number of items read is correct.

    Tim S.
    Last edited by stahta01; 08-08-2012 at 06:48 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  2. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM