C语言 C scanf 语法帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3723768/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:27:03  来源:igfitidea点击:

Help with C scanf syntax

csyntax

提问by rectangletangle

When I run the following snippet it runs until the second question. It then puts the "Is the customer a student? (y/n) \n" and "What is the movies time? (in hours) \n" prompts together (no area to answer between them). If one takes any action from there on, the program stops working. What did I do wrong? (i'm pretty sure it's syntax related)

当我运行以下代码段时,它会一直运行到第二个问题。然后它将“客户是学生吗?(y/n)\n”和“电影时间是什么?(以小时为单位)\n”提示放在一起(它们之间没有区域可以回答)。如果从那以后采取任何行动,程序就会停止工作。我做错了什么?(我很确定它与语法有关)

int A,B,C,D,age,time;
char edu, ddd;

printf ("What is the customer's age? \n");
scanf("%d", &age);

printf ("Is the customer a student? (y/n) \n");
scanf("%c", &edu);

printf ("What is the movies time? (in hours) \n");
scanf("%d", &time);

printf ("Is the movie 3-D? (y/n) \n");
scanf("%c", &ddd);

回答by codaddict

When reading input using scanf, the input is read after the return key is pressed but the newline generated by the return key is not consumed by scanf, which means the next time you read from standard input there will be a newline ready to be read.

使用 读取输入时scanf,按下回车键后读取输入,但回车键生成的换行符不会被 消耗scanf,这意味着下次从标准输入读取时,将有一个换行符可供读取。

One way to avoid is to use fgetsto read the input as a string and then extract what you want using sscanf.

避免的一种方法是使用fgets将输入作为字符串读取,然后使用sscanf.

Another way to consume the newline would be to scanf("%c%*c",&edu);. The %*cwill read the newline from the buffer and discard it.

使用换行符的另一种方法是scanf("%c%*c",&edu);. 该%*c会从缓冲器读取换行符并将其丢弃。

回答by Matthew Iselin

You probably need to eat the extra input from stdin after each scanf so it doesn't stick around in the buffer and cause scanf to receive the buffered data.

您可能需要在每次 scanf 之后吃掉来自 stdin 的额外输入,这样它就不会留在缓冲区中并导致 scanf 接收缓冲数据。

This is because the newline from hitting enter after the first text entry stays in the buffer and is a valid entry for the "%c" format - if you look at the value of "edu" you should find it's a newline character.

这是因为在第一个文本条目之后按 Enter 的换行符保留在缓冲区中,并且是“%c”格式的有效条目——如果您查看“edu”的值,您应该会发现它是一个换行符。

回答by Matthew Flaschen

You can add a space before the %c. This is necessary because unlike other conversion specifiers, it doesn't skip whitespace. So when the user enters something like "10\n" as the age, the first scanfreads up to the end of 10. Then, the %c reads the newline. The space tells scanfto skip all the current whitespace before reading a character.

您可以在 %c 之前添加一个空格。这是必要的,因为与其他转换说明符不同,它不会跳过空格。所以当用户输入类似 "10\n" 作为年龄时,第一个scanf读取到 10 的末尾。然后,%c 读取换行符。空格告诉scanf在读取字符之前跳过所有当前空格。

printf ("What is the customer's age? \n");
scanf("%d", &age);

printf ("Is the customer a student? (y/n) \n");
scanf(" %c", &edu);

printf ("What is the movies time? (in hours) \n");
scanf("%d", &time);

printf ("Is the movie 3-D? (y/n) \n");
scanf(" %c", &ddd);

回答by user411313

There are any problems with scanf and "%c", see eg: @jamesdlin. "time" is the name of a C-Standard-Lib function, better you use a different name, eg:

scanf 和 "%c" 有任何问题,请参见例如:@jamesdlin。“时间”是 C-Standard-Lib 函数的名称,最好使用不同的名称,例如:

int A,B,C,D,age=0,timevar=0;
char edu=0, ddd=0, line[40];

printf ("What is the customer's age? \n");
if( fgets(line,40,stdin) && 1!=sscanf(line,"%d", &age) ) age=0;

printf ("Is the customer a student? (y/n) \n");
if( fgets(line,40,stdin) && 1!=sscanf(line,"%c", &edu) ) edu=0;

printf ("What is the movies time? (in hours) \n");
if( fgets(line,40,stdin) && 1!=sscanf(line,"%d", &timevar) ) timevar=0;

printf ("Is the movie 3-D? (y/n) \n");
if( fgets(line,40,stdin) && 1!=sscanf(line,"%c", &ddd) ) ddd=0;

At the end your vars have a defined content, 0 for an input-error, !=0 otherwise.

最后,你的变量有一个定义的内容,0 表示输入错误,否则 !=0。

回答by visha

Use the fflush(stdin);

使用 fflush(stdin);

statement to clear the buffer memory of stdin before reading any character data

在读取任何字符数据之前清除 stdin 缓冲存储器的语句

or else it will read the enter key value of first scanf to the second scanf.

否则它将读取第一个 scanf 的输入键值到第二个 scanf。

回答by Kevin

I tried your program and it seems that after typing the age, when I press enter, it considers that as the input for the next scanf (i.e. for &edu) and similarly for third and fourth question. My solution could be naive but you could simply use a buffer scanf after each one to absorb the "Enter". Or simply do this

我试过你的程序,似乎在输入年龄后,当我按下回车键时,它认为这是下一个 scanf(即 &edu)的输入,第三和第四个问题也类似。我的解决方案可能很幼稚,但您可以简单地在每个解决方案之后使用缓冲区 scanf 来吸收“Enter”。或者干脆这样做

scanf(" %c", &variable);

(Any whitespace in the format string will make scanf absorb all further consecutive whitespace).

(格式字符串中的任何空格将使 scanf 吸收所有进一步的连续空格)。