?
Solved

why the EOF character cannot be detected?

Posted on 2003-03-23
5
Medium Priority
?
272 Views
Last Modified: 2010-04-15
this is a part of the program i write... the while loop should stop when i press ctrl+z.. but it's not to be so..

     int x=0,y=0;
     int min=0,max=0,avr=0;
     int sum=0;
     
     while(x!=EOF)
     {
          scanf("%d",&x);
          if(x<min)
               min=x;
         
          if(x>max)
               max=x;
         
          sum=sum+x;
          y++;
          avr=sum/y;
     }
0
Comment
Question by:A_D
5 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8193250
The problem is that you've declared 'x' as an integer and trying to read integers and characters (like the EOF) character. I bet if you enter -1 as the value of x, then your while loop will stop.

If you want to type Ctrl-Z and exit the loop, then you better declare 'x' as a character and read it using "%c", and then convert it to its integer equivalent to add to sum. However, that'll create problems while entering numbers with more than 1 digit. So I guess the best way to do it is to read it as an integer and then ask the user if the user wishes to continue or not, as:

char choice = 'y' ;
int x ;

while ( choice = 'y' || choice == 'Y' )
{
  scanf ( "%d", &x ) ;
..
..
  printf ( "\n Continue (y/n): " ) ;
  fflush ( stdin ) ; // clear the input buffer - might be required in some cases
  ch = getchar () ;

} // end while


Mayank.
0
 

Accepted Solution

by:
Getch earned 100 total points
ID: 8193661
Look if you use unix/linux the EOF Character is ctrl-d
if you use windows is ctrl-z
so try this.

#include <stdio.h>
#define EOF(-1) //Dos/Windows

void main(void) {
 int x=0,y=0;
    int min=0,max=0,avr=0;
    int sum=0;
   
    while(x!=EOF)
    {
         scanf("%d",&x);
         if(x<min)
              min=x;
         
         if(x>max)
              max=x;
         
         sum=sum+x;
         y++;
         avr=sum/y;
    }
}

i'm not sure that will work. but you use scanf("%d",&x);
this will input an integer... huh look here
#include <stdio.h>
#define EOF(-1) //Dos/Windows

void main(void) {
 int x=0,y=0;
    int min=0,max=0,avr=0;
    int sum=0;
    char ch;
   
    while((ch = getchar()) !=EOF)
    {
         scanf("%d",&x);
         if(x<min)
              min=x;
         
         if(x>max)
              max=x;
         
         sum=sum+x;
         y++;
         avr=sum/y;
    }
}
this will work.You know wat getchar() is right ???
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8193715
EOF is returned by scanf, so your code should be

    int x=0,y=0;
    int min=0,max=0,avr=0;
    int sum=0;
   
    while(scanf("%d",&x) != EOF) {
         if(x<min)
              min=x;
         
         if(x>max)
              max=x;
         
         sum=sum+x;
         y++;
         avr=sum/y;
    }
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8193744
Dear Kocil,

He wants his program to stop when the user presses Ctrl-Z, as per what's mentioned in the question. Do you think that scanf () will detect it that way??

rgds,

Mayank.
0
 

Expert Comment

by:posternb
ID: 8195759
Kocil is right, scanf will return EOF.  Alternatively you could test feof(stdin)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question