0
   int a,b;
    while (scanf("%d",&a) != EOF){
        printf("%d ",a);
    }
    printf("\n");
    printf("Pls enter value b\n");
    scanf("%d",&b);
    printf("%d",b);
    return 0;

When I type and then use command + D to jump out of the while loop, I cannot enter it again at this time, resulting in the value of b being random. enter image description here

苏凯承
  • 51
  • 1
  • 8
  • Maybe this is a problem with your IDE, try this on a proper shell. It worked just fine for me on my terminal. – Unmanned Player Jan 19 '20 at 21:39
  • It would be better to use some other way to terminate the first loop -- it's not really portable to close the input this way and then somehow reactivate it – M.M Jan 19 '20 at 21:40
  • 4
    Ctrl-D signals End of File. What do you think happens when you read from a file handle that has reached EOF? – ikegami Jan 19 '20 at 21:40
  • @ikegami it's not always so simple, see here: https://stackoverflow.com/questions/29759979/why-do-i-have-to-input-eof-3-times-when-using-fgets . Also I am not even sure my answer is correct, I think case 2 is actually causing a read error without closing the stream. None of this is covered by the standard either – M.M Jan 19 '20 at 22:04
  • @M.M I'm well aware of that, but clearing the EOF file is not the solution, and I purposefully avoided mentioning that. The correct solution would involve an approach that doesn't require clearing (or really, even signalling) EOF. For example, reading a line and scanning that for the "a" values. – ikegami Jan 19 '20 at 22:05

2 Answers2

0

As other have suggested , you should find another way to terminate your loop. Although , here is a workaround:

#include <stdio.h>

int main(){
 int a,b;
    while (scanf("%d",&a)!=EOF){
        printf("%d ",a);
    }
    printf("\n");
    printf("Pls enter value b\n");
    freopen("/dev/tty", "r", stdin); /* Change /dev/tty to con: if you are using windows */
    scanf("%d",&b);
    printf("%d",b);
    return 0;

}

You can use freopen to force input from the console.

alex01011
  • 1,670
  • 2
  • 5
  • 17
0

I got a mail from IntelliJ , You can see it. I type the links so that you can access link. email-content: There is an issue with sending EOF in CLion: https://youtrack.jetbrains.com/issue/CPP-5704. Disabling run.processes.with.pty is a workaround which helps to get the output printed after EOF. But this workaround has downsides like the one you faced with using scanf() after EOF. Unfortunately, there is no way to get both the output after EOF and scanf() after EOF working correctly until the issue (https://youtrack.jetbrains.com/issue/CPP-5704) is resolved. Feel free to comment or upvote the issue in order to get updates. See https://intellij-support.jetbrains.com/hc/en-us/articles/207241135-How-to-follow-YouTrack-issues-and-receive-notifications if you are not familiar with YouTrack.

Sorry for the inconvenience.

Best regards, Anna Falevskaya JetBrains http://www.jetbrains.com The Drive to Develop

enter image description here

苏凯承
  • 51
  • 1
  • 8