Get scanf to quit when it reads a newline?

后端 未结 5 1438
轻奢々
轻奢々 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 10:16

    Use fgets to read console input:

       int res = 2;
       while (res == 2) {
           char buf[100];
           fgets(buf, sizeof(buf), stdin);
           res = sscanf(buf, "%f %f", &real, &img);
           if (res == 2)
               c[i++] = real + img * I;
       }
       c[i++] = 1 + 0*I; // most significant coefficient is assumed to be 1
       return i;
    

提交回复
热议问题