简体   繁体   中英

How can I make scanf () to receive the value of b?

   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. 在此处输入图片说明

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM