?
Solved

why the EOF character cannot be detected?

Posted on 2003-03-23
5
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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

801 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