fflush() is not working in Linux

前端 未结 9 787
后悔当初
后悔当初 2020-12-08 05:43

I used the fflush() in Linux GCC but it did not work. Are there any alternatives for that function? Here is my code:

#include
void main()
{
           


        
相关标签:
9条回答
  • 2020-12-08 06:40
    #include<stdio.h>
    int main()
    {
    char ans='y';
    int a;
    while(ans=='y'||ans=='Y')
        {
    
         printf("Type a number:-");
         scanf("%d",&a);
         printf("square of number = %d\nwant to enter 
         number again(y/n)?\nANS=",a*a); 
         scanf("%s",&ans);//use %s in place of %c
         }
    return 0;
    }
    
    0 讨论(0)
  • 2020-12-08 06:43

    Use getchar() instead, after scanf

    0 讨论(0)
  • 2020-12-08 06:44

    One that always works on Linux:

    #include <termios.h>
    #include <unistd.h>
    
    void clean_stdin()
    {
            int stdin_copy = dup(STDIN_FILENO);
            /* remove garbage from stdin */
            tcdrain(stdin_copy);
            tcflush(stdin_copy, TCIFLUSH);
            close(stdin_copy);
    }
    

    You can use tcdrain and tcflush not only for in/out/err fd.

    0 讨论(0)
提交回复
热议问题