C语言 在获取之前在 C. Scanf 中输入。问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2366509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input in C. Scanf before gets. Problem
提问by Dmitri
I'm pretty new to C, and I have a problem with inputing data to the program.
我对 C 很陌生,我在向程序输入数据时遇到了问题。
My code:
我的代码:
#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;
}
It allows to input ID, but it just skips the rest of the input. If I change the order like this:
它允许输入 ID,但它只是跳过输入的其余部分。如果我像这样更改顺序:
printf("Input your name: ");
gets(b);
printf("Input your ID: ");
scanf("%d", &a);
It will work. Although, I CANNOT change order and I need it just as-is. Can someone help me ? Maybe I need to use some other functions. Thanks!
它会起作用。虽然,我不能改变订单,我需要它原样。有人能帮我吗 ?也许我需要使用其他一些功能。谢谢!
回答by IVlad
Try:
尝试:
scanf("%d\n", &a);
gets only reads the '\n' that scanf leaves in. Also, you should use fgets not gets: http://www.cplusplus.com/reference/clibrary/cstdio/fgets/to avoid possible buffer overflows.
gets 只读取 scanf 留下的 '\n'。此外,您应该使用 fgets 而不是 gets:http: //www.cplusplus.com/reference/clibrary/cstdio/fgets/以避免可能的缓冲区溢出。
Edit:
编辑:
if the above doesn't work, try:
如果上述方法不起作用,请尝试:
...
scanf("%d", &a);
getc(stdin);
...
回答by AndiDog
scanfdoesn't consume the newline and is thus a natural enemy of fgets. Don't put them together without a good hack. Both of these options will work:
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
回答by vinayak
scanf will not consume \n so it will be taken by the gets which follows the scanf. flush the input stream after scanf like this.
scanf 不会消耗 \n,所以它会被 scanf 后面的 get 占用。像这样在 scanf 之后刷新输入流。
#include <stdlib.h>
#include <string.h>
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;
}
回答by muruga
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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.
回答by Madan Ram
you should do this way.
你应该这样做。
fgetc(stdin);
scanf("%c",&c);
if(c!='y')
{
break;
}
fgetc(stdin);
to read input from scanf after reading through gets.
在通读gets后从scanf读取输入。
回答by Isaac Balintuma
Just use 2 gets() functions
只需使用 2 个 get() 函数
When you want to use gets() after a scanf(), you make sure that you use 2 of the gets() functions and for the above case write your code like:
当您想在 scanf() 之后使用 get() 时,请确保使用 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;
}
For explanation ([email protected]);
解释([email protected]);
回答by Rogério De Leon Pereira
scanf("%d", &a);can't read the return, because %daccepts only decimal integer. So you add a \nat the beginning of the next scanfto ignore the last \ninside the buffer.
scanf("%d", &a);无法读取返回值,因为%d只接受十进制整数。因此,您\n在 next 的开头添加一个scanf以忽略\n缓冲区内的最后一个。
Then, scanf("\n%s", b);now can reads the string without problem, but scanfstops to read when find a white space. So, change the %sto %[^\n]. It means: "read everthing but \n"
然后,scanf("\n%s", b);现在可以毫无问题地读取字符串,但scanf在找到空格时停止读取。因此,将 更改%s为%[^\n]。它的意思是:“阅读除此之外的所有内容\n”
scanf("\n%[^\n]", b);
scanf("\n%[^\n]", b);
#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: ");
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;
}
回答by Duy ??ng
The scanffunction removes whitespace automatically before trying to parse things other than characters. %c, %n, %[]are exceptions that do not remove leading whitespace.getsis reading the newline left by previous scanf. Catch the newline usinggetchar();
该scanf函数会在尝试解析字符以外的内容之前自动删除空格。%c, %n,%[]是不删除前导空格的例外。gets正在阅读 previous 留下的换行符scanf。使用捕获换行符getchar();
scanf("%d", &a);
getchar(); // catches the newline character omitted by scanf("%d")
gets(b);

