0

代码是检查是否有空行。我想当我输入一些文本时,它会继续执行 printf(),因为它卡在循环中。但实际上,它只是执行 printf() 一次,然后等待另一行文本。为什么?是因为在gets()函数之后输入将被擦除吗?

这是代码

int main(){
    char input[257];
    char *ptr;

    puts("Enter text a line at a time, then press Enter");
    puts("Enter a blank line when done");

    while( *(ptr= gets(input)) != NULL){
        printf("You've entered: %s\n", input);
    }
    puts("Thank you and goodbye\n");

    return 0;
}
4

1 回答 1

1

这时候应该可以解决问题

while( (ptr= gets(input)) != NULL && input[0]!='\0')
于 2015-12-01T10:49:27.373 回答