在获取之前输入C. Scanf。 问题

我是C的新手,我在向程序输入数据时遇到了问题。

我的代码:

#include  #include  #include  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); 

它会工作。 虽然,我不能改变秩序,我需要它原样。 有人能帮我吗 ? 也许我需要使用其他一些function。 谢谢!

尝试:

 scanf("%d\n", &a); 

获取只读取scanf离开的’\ n’。此外,您应该使用fgets not gets: http : //www.cplusplus.com/reference/clibrary/cstdio/fgets/以避免可能的缓冲区溢出。

编辑:

如果上述方法无效,请尝试:

 ... scanf("%d", &a); getc(stdin); ... 

scanf不消耗换行符,因此是fgets天敌。 没有好的黑客,不要把它们放在一起。 这两个选项都有效:

 // Option 1 - eat the newline scanf("%d", &a); getchar(); // reads the newline character // Option 2 - use fgets, then scan what was read char tmp[50]; fgets(tmp, 50, stdin); sscanf(tmp, "%d", &a); // note that you might have read too many characters at this point and // must interprete them, too 

scanf不会消耗\ n因此它将被scanf后面的gets所占用。 像scanf一样在scanf之后刷新输入流。

 #include  #include  int main(void) { int a; char b[20]; printf("Input your ID: "); scanf("%d", &a); fflush(stdin); printf("Input your name: "); gets(b); printf("---------"); printf("Name: %s", b); system("pause"); return 0; } 
 #include  #include  #include  int main(void) { int a; char b[20]; printf("Input your ID: "); scanf("%d", &a); getchar(); printf("Input your name: "); gets(b); printf("---------"); printf("Name: %s", b); return 0; } Note: If you use the scanf first and the fgets second, it will give problem only. It will not read the second character for the gets function. If you press enter, after give the input for scanf, that enter character will be consider as a input f or fgets. 

你应该这样做。

  fgetc(stdin); scanf("%c",&c); if(c!='y') { break; } fgetc(stdin); 

通过读取后读取scanf的输入。

只需使用2个gets()函数

当你想在scanf()之后使用gets()时,你要确保使用2个gets()函数,对于上面的例子,你可以编写代码:

 int main(void) { int a; char b[20]; printf("Input your ID: "); scanf("%d", &a); //the change is here********************* printf("Input your name: "); gets(b); gets(b); //the change is here********************* printf("---------"); printf("Name: %s", b); system("pause"); return 0; } 

有关解释(isaaconline96@gmail.com);

scanf("%d", &a); 无法读取返回值,因为%d只接受十进制整数。 所以你在下一个scanf的开头添加一个\n来忽略缓冲区中的最后一个\n

然后, scanf("\n%s", b); 现在可以毫无问题地读取字符串,但是当找到空格时, scanf停止读取。 因此,将%s更改为%[^\n] 。 这意味着:“阅读everthing但是\n

scanf("\n%[^\n]", b);

 #include  #include  #include  int main(void) { int a; char b[20]; printf("Input your ID: "); scanf("%d", &a); printf("Input your name: "); scanf("\n%[^\n]", b); //first \n says to ignore last 'return' //%[^\n] read until find a 'return' printf("---------\n"); printf("Name: %s\n\n", b); system("pause"); return 0; } 

scanf函数在尝试解析字符以外的内容之前会自动删除空格。 %c%n%[]是不删除前导空格的exception。

gets正在读取前一个scanf留下的换行符。 使用getchar();捕获换行符getchar();

 scanf("%d", &a); getchar(); // catches the newline character omitted by scanf("%d") gets(b); 

https://wpollock.com/CPlus/PrintfRef.htm