Get scanf to quit when it reads a newline?

后端 未结 5 1453
轻奢々
轻奢々 2020-11-30 09:27

If I input 5 5 at the terminal, press enter, and press enter again, I want to exit out of the loop.

int readCoefficents(double complex *c){
             


        
5条回答
  •  囚心锁ツ
    2020-11-30 09:56

    re PAXDIABLO solution: it does not work properly with EMPTY line entered by user, so this line shall be added in your getLine() function

    if (strlen(buff) <= 1) return NO_INPUT;
    

    after the line:

    if (fgets (buff, sz, stdin) == NULL)
        return NO_INPUT;
    

    So it will become :

    ...
       if (strlen(buff) <= 1) return NO_INPUT;
       if (fgets (buff, sz, stdin) == NULL) return NO_INPUT;
    ....
    

提交回复
热议问题