我在使用C语言时遇到问题。
...
int main()
{
char test[20], m[20];
int n;
scanf("%d", &n);
while(n)
{
gets(test);
test.kolona = n;
m = decode(test); //some function
printf("%s",m.sif);
putchar('\n');
scanf("%d", &n);
}
}
当我输入数字并按回车键时,它会在您输入字符串之前自动“打印”换行符。我搜索了一下,发现这可以避免,如果你之前放了一个,像这样:
...
scanf("%d", &n);
gets(test)
while(n);
{
gets(test);
...
}
但随着循环的继续,它再次混乱:(
这有一个优雅的解决方案吗?
答案 0 :(得分:0)
要修复的示例
int main()
{
char test[20], m[20];
int n;
scanf("%d%*c", &n);//skip the newline following the numeric input
while(n)
{
scanf("%19[\^n]", test);//gets has been abolished.
//test.kolona = n;//The array does not have a member field.
//strcpy(m, decode(test));//m = decode(test); //Can not be assigned to the array in this way
printf("%s\n", m);
//putchar('\n');
scanf("%d%*c", &n);
}
}