我对C语言还比较陌生,我在输入程序数据时遇到了问题。
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
printf("Input your name: ");
gets(b);
printf("---------");
printf("Name: %s", b);
system("pause");
return 0;
}
它允许输入ID,但它只跳过了其余的输入。如果我像这样更改顺序:
printf("Input your name: ");
gets(b);
printf("Input your ID: ");
scanf("%d", &a);
它会起作用。尽管如此,我不能更改顺序,我需要保持原样。有人能帮助我吗?也许我需要使用其他函数。谢谢!
gets
函数,会有狂暴的攻击性美洲豹扑向你并撕开你的眼窝。因此,请不要使用该函数。 - Tyler McHenryscanf
是邪恶的 - http://c-faq.com/stdio/scanfprobs.html - jschmiergets(b);
change toscanf(" %19[^\n]", b);
- BLUEPIXY