2
    int x,y,m;
for(;;){
    m=scanf("%d %d",&x,&y);
    if (m!=2 || m==EOF){
        break;
    }
    else{
        printf("/%d/%d/\n",x,y);
    }
}
if (feof ( stdin )){
  printf("End of input\n");
}else if(m!=2){
  printf("There was an error\n");
}

Under linux ctrl+D indicates end of input , and for windows ctrl+z is supposed to do the trick, but it doesn't work. Any ideas?

Randalfien
  • 396
  • 2
  • 3
  • 17
  • possible duplicate of [What is EOF in the C programming language?](http://stackoverflow.com/questions/1782080/what-is-eof-in-the-c-programming-language) – Fred Foo Oct 26 '10 at 12:04

2 Answers2

5

Try pressing Enter after Ctrl+z

If still no luck, please try the C++ version:

#include <iostream>

int x, y;
while ( std::cin >> x >> y )
   std::cout << '/' << x << '/' << y << "/\n";
if ( std::cin.eof() )
   std::cout << "End of input\n";
else
   std::cout << "There was an error\n";

and see if it does better?

usta
  • 6,699
  • 3
  • 22
  • 39
  • Nope, doesn't work. it just freezes after ctrl+z, no more input possible, not even enter. – Randalfien Oct 26 '10 at 12:21
  • BTW do you think that scanf doesnt return EOF ? cause all the examples from web work with getchar ..but since I need integers , I don't want to use that.. – Randalfien Oct 26 '10 at 12:37
  • @Randalfien: scanf will return EOF when end-of-file is occurred. – usta Oct 26 '10 at 12:57
  • @Randalfien: Ctrl+z followed by Enter works for me with your code, VC 10 Express and XP SP3. What compiler do you use? – usta Oct 26 '10 at 12:59
  • I use NetBeans, thats gcc compiler , I run Win7 .. Can it be, that I have something wrong with my compiler comfiguration? – Randalfien Oct 26 '10 at 13:56
  • @Randalfien: Let's do a quick check with C++. Please see my edited answer for that. – usta Oct 26 '10 at 14:21
  • Ok , I've typed in your code and it acts just like before. When I press ctrl z , enter it does nothing, and input is no longer possible, I presume it works for you just fine, right? so it must be some bad configuration ? (BTW thanks very much for all the advice !) – Randalfien Oct 26 '10 at 16:35
  • @Randalfien: Yes, they work fine with my configuration. I'll try to find some time to try this with gcc under windows. That is weird... – usta Oct 26 '10 at 17:37
  • @Randalfien: I checked with mingw, gcc 4.3.0 on Windows XP SP2 and both of the examples worked just fine -- after Ctrl-z followed by Enter "End of input" was printed and program exited. So that must be an environment/configuration issue I guess. – usta Oct 27 '10 at 08:50
-2
#include<stdio.h>
#include<conio.h>
void main (void)
{
int x,y,m;
for(x=0;x>=0;x++){
    m=scanf("%d %d",&x,&y);
    if (m!=2 || m==EOF){
    break;
    }
    else printf("/%d/%d/\n",x,y);
}
if (feof ( stdin )){
  printf("End of input\n");
}
else if(m!=2){
  printf("There was an error\n");
}
getch();
}
Remy
  • 1
  • 2