#include <stdio.h>
#include <string.h>

int main(void) {

    int i, j, t;
    scanf("%d", &t); // enter the number of test cases
    getchar();
    char input[11111];
    for(i=0; i<t; i++){
        scanf("%[^STOP]", input); // take input till STOP will come
        printf("%s\n", input);
// this code print first input as many time as number of test cases in the code that will we provide through variable t;
    }
    return 0;
}

最佳答案

将您的代码更改为

#include <stdio.h>
#include <string.h>

int main(void) {

    int i, j, t;
    char ch;
    scanf("%d", &t); // enter the number of test cases
    getchar();
    char input[11111];
    for(i=0; i<t; i++){
        scanf("%[^STOP]", input); // take input till STOP will come
        while((ch=getchar())!='\n'&& ch!= EOF);
        printf("%s\n", input);
// this code print first input as many time as number of test cases in the code that will we provide through variable t;
    }
    return 0;
}


该问题是由于缓冲引起的。 scanf()\n发送到输入缓冲区,下一个scanf()读取此内容。这就是造成问题的原因。

如果您想了解更多信息,只需在Stack Overflow或Google中搜索输入缓冲区即可。已经多次讨论了这个问题。

10-08 07:36
查看更多