BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #21202同步于 2007/9/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖

[合集] scanf()为什么没法执行?

CNLAS
2007/9/20镜像同步0 回复
☆─────────────────────────────────────☆ wujinhua (开杞佩) 于 (Thu Sep 13 10:30:38 2007) 提到: printf("请输入查询方式:\n"); scanf("%c",&k); printf("woeo"); 上下两句都执行 ,为什么中间这句不行,换用getchar()输入一个字符,也无法执行~~~~? ☆─────────────────────────────────────☆ wujinhua (开杞佩) 于 (Thu Sep 13 10:34:00 2007) 提到: 试了下把k改成字符数组再输入就能了~~为什么? 【 在 wujinhua 的大作中提到: 】 : printf("请输入查询方式:\n"); : scanf("%c",&k); : printf("woeo"); : ................... ☆─────────────────────────────────────☆ coolfantasy (Cool) 于 (Thu Sep 13 10:35:58 2007) 提到: 【 在 wujinhua (开杞佩) 的大作中提到: 】 : 标 题: scanf()为什么没法执行? : 发信站: 北邮人论坛 (Thu Sep 13 10:30:38 2007), 站内 : : printf("请输入查询方式:\n"); getchar(); : scanf("%c",&k); : printf("woeo"); : 上下两句都执行 ,为什么中间这句不行,换用getchar()输入一个字符,也无法执行~~~~? : : -- : : ※ 来源:·北邮人论坛 http://forum.byr.edu.cn·[FROM: 59.64.225.*] ☆─────────────────────────────────────☆ fliger (拾笔从学) 于 (Thu Sep 13 11:07:22 2007) 提到: 【 在 wujinhua 的大作中提到: 】 : printf("请输入查询方式:\n"); : scanf("%c",&k); : printf("woeo"); : ................... 是 请输入查询方式: 刚输出紧接着woeo就输出了吗? ☆─────────────────────────────────────☆ wujinhua (开杞佩) 于 (Thu Sep 13 12:01:06 2007) 提到: 恩 是的 【 在 fliger 的大作中提到: 】 : 是 请输入查询方式: 刚输出紧接着woeo就输出了吗? ☆─────────────────────────────────────☆ CNLAS (Ich gewinne bestimmt……) 于 (Thu Sep 13 13:11:35 2007) 提到: 你debug看看k是啥值就明白了。。 [QUOTE]这里是我错了。。。更正在10楼 scanf会自动把上一行printf打印出来的\n读进去。。。所以cool在2楼的意思是在printf和scanf中间加一个getchar把\n吃掉。。[/QUOTE] 或者用滞后赋值把scanf改成scanf("%*c%c",&k)也可以 ☆─────────────────────────────────────☆ wujinhua (开杞佩) 于 (Thu Sep 13 13:35:00 2007) 提到: 对 试了下把j改称整型也可以 printf("继续查询请按1,退出查询请按其他键:\n"); scanf("%d",&j); ☆─────────────────────────────────────☆ Lynus (爱生活,爱Linux) 于 (Thu Sep 13 15:13:09 2007) 提到: 【 在 CNLAS 的大作中提到: 】 : 你debug看看k是啥值就明白了。。 : scanf会自动把上一行printf打印出来的\n读进去。。。所以cool在2楼的意思是在printf和scanf中间加一个getchar把\n吃掉。。 : 或者用滞后赋值把scanf改成scanf("%*c%c",&k)也可以 我不是很理解。输入输出流怎么会混在一起呢? ☆─────────────────────────────────────☆ JackSparrow (CaptainJackSparrow) 于 (Thu Sep 13 15:18:53 2007) 提到: 同不明白 【 在 Lynus 的大作中提到: 】 : 我不是很理解。输入输出流怎么会混在一起呢? ☆─────────────────────────────────────☆ wujinhua (开杞佩) 于 (Thu Sep 13 15:36:22 2007) 提到: 不过用int时后面又有东西显示不出来~~ 【 在 CNLAS 的大作中提到: 】 : 你debug看看k是啥值就明白了。。 : scanf会自动把上一行printf打印出来的\n读进去。。。所以cool在2楼的意思是在printf和scanf中间加一个getchar把\n吃掉。。 : 或者用滞后赋值把scanf改成scanf("%*c%c",&k)也可以 ☆─────────────────────────────────────☆ CNLAS (Ich gewinne bestimmt……) 于 (Thu Sep 13 15:38:57 2007) 提到: -_______________- 多谢指正。。。。忘了考虑这个了。。。原来我一直的理解都是错的。。。= = 刚才用vc和gcc验证了一下。。。不是输出流的\n 而是在结束上一次的scanf输入的时候按的回车。。。 这样这个回车就被放到输入缓冲里了。。。而且vc和gcc放进去的都是0A(10) 【 在 Lynus 的大作中提到: 】 : 我不是很理解。输入输出流怎么会混在一起呢? ☆─────────────────────────────────────☆ UnrealT (UT) 于 (Thu Sep 13 15:54:04 2007) 提到: 还是\n ☆─────────────────────────────────────☆ wks (cloverprince) 于 (Thu Sep 13 18:19:26 2007) 提到: 宁可这样: char buffer[256]; printf("Are you sure?(y/n)\n"); scanf("%s",buffer); if(buffer[0]=='y') printf("ok!\n"); else printf("cancelled\n"); ☆─────────────────────────────────────☆ w1508 (阿斗) 于 (Thu Sep 13 21:22:58 2007) 提到: 【 在 CNLAS 的大作中提到: 】 : 你debug看看k是啥值就明白了。。 : [QUOTE]这里是我错了。。。更正在10楼 : scanf会自动把上一行printf打印出来的\n读进去。。。所以cool在2楼的意思是在printf和scanf中间加一个getchar把\n吃掉。。[/QUOTE] : ................... 什么叫滞后赋值? ☆─────────────────────────────────────☆ CNLAS (Ich gewinne bestimmt……) 于 (Thu Sep 13 21:31:27 2007) 提到: 就是从流里面吃掉一个。。。不赋给变量。。。随便找本c的书应该都有啊。。。 【 在 w1508 的大作中提到: 】 : 什么叫滞后赋值? ☆─────────────────────────────────────☆ CNLAS (Ich gewinne bestimmt……) 于 (Thu Sep 13 21:50:22 2007) 提到: msdn里的。。。 [QUOTE]Run-Time Library Reference Format Specification Fields: scanf and wscanf Functions The information here applies to the entire scanf family of functions, including the secure versions and describes the symbols used to tell the scanf functions how to parse the input stream, such as the input stream stdin for scanf, into values that are inserted into program variables. A format specification has the following form: %[*] [width] [{h | l | ll | I64 | L}]type The format argument specifies the interpretation of the input and can contain one or more of the following: White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input. Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in the input stream does not match, scanf terminates. Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list. The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in the input stream; the matching characters in the input stream are scanned but not stored. If a character in the input stream conflicts with the format specification, scanf terminates, and the character is left in the input stream as if it had not been read. When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string. An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification. Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%. An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored. The secure versions (those with the _s suffix) of the scanf family of functions require that a buffer size parameter be passed preceding each parameter of type c, C, s, S or [.[/QUOTE] 【 在 CNLAS 的大作中提到: 】 : 就是从流里面吃掉一个。。。不赋给变量。。。随便找本c的书应该都有啊。。。 ☆─────────────────────────────────────☆ nvidia (Geforce 8800GTX) 于 (Thu Sep 13 22:02:42 2007) 提到: char sz[2]; printf("Are you sure?:\n"); fscanf(stdin, "%s", sz); assert (strlen(sz) == 1); 下面判断sz[0]即可 ☆─────────────────────────────────────☆ nvidia (Geforce 8800GTX) 于 (Thu Sep 13 22:23:25 2007) 提到: 在scanf()之后加一语句fflush(stdin);即可 ☆─────────────────────────────────────☆ mimi7751 (暖暖) 于 (Thu Sep 13 22:51:11 2007) 提到: 我上次就遇到这个问题,不过很弱智的解决了,写两句scanf就可以了,小女子也不明白原因,还望高手莫取笑。 ☆─────────────────────────────────────☆ CNLAS (Ich gewinne bestimmt……) 于 (Thu Sep 13 23:07:05 2007) 提到: 另——老外们讨论的同一个问题。。。供大家参考。。。 http://www.linuxforums.org/forum/linux-programming-scripting/67560-problem-scanf.html ☆─────────────────────────────────────☆ akeybupt2004 (最爱米莱) 于 (Thu Sep 13 23:09:15 2007) 提到: 【 在 CNLAS 的大作中提到: 】 : -_______________- : 多谢指正。。。。忘了考虑这个了。。。原来我一直的理解都是错的。。。= = : 刚才用vc和gcc验证了一下。。。不是输出流的\n : ................... 奇怪,为啥我的就没lz出现的问题呢?? [upload=1][/upload] ☆─────────────────────────────────────☆ CNLAS (Ich gewinne bestimmt……) 于 (Thu Sep 13 23:16:52 2007) 提到: 前面再加个scanf 【 在 akeybupt2004 的大作中提到: 】 : 奇怪,为啥我的就没lz出现的问题呢?? : ☆─────────────────────────────────────☆ LoveRose (江南西道|tuoqi-开始还债) 于 (Sun Sep 16 00:06:29 2007) 提到: 前面加个 scanf把\n吃掉 ☆─────────────────────────────────────☆ daHe (一条大河) 于 (Sun Sep 16 17:16:41 2007) 提到: 【 在 CNLAS 的大作中提到: 】 : 你debug看看k是啥值就明白了。。 : [QUOTE]这里是我错了。。。更正在10楼 : scanf会自动把上一行printf打印出来的\n读进去。。。所以cool在2楼的意思是在printf和scanf中间加一个getchar把\n吃掉。。[/QUOTE] : ................... 请问你这种引用方式是怎么实现的? ☆─────────────────────────────────────☆ gkso (myth) 于 (Sun Sep 16 17:56:00 2007) 提到: fflush(stdin); ☆─────────────────────────────────────☆ rabbit (贝贝) 于 (Mon Sep 17 00:09:50 2007) 提到: 当你使用scanf和getchar来获取输入时,虽然最后的回车不会被读到数据中,但是这个回车是被存储到stdin缓存中,当再一次使用scanf来获取输入时就会先把stdin缓存中的数据读出,所以才出现你说的那种情况,如果是先是用gets,就不会出现这种情况。所以在再次输入之前要用fflush(stdin)来清空缓存。 #include <stdio.h> int main(){ char k; scanf("%c",&k); printf(" k1 = %d\n",k); printf("please input: \n"); scanf("%c",&k); printf(" k2 = %d\n",k); printf("thank you"); return 0; } 当第一次输入a之后,就会看到k1 = 97,k2 = 10 ☆─────────────────────────────────────☆ ericyosho (ericyosho) 于 (Tue Sep 18 17:56:24 2007) 提到: fflush(stdin)在windows下是可以这样用的, 但根据C标准,fflush()作用于输入流结果不确定,代码的可移植性也是有问题的。 一般的做法是在每一个scanf语句后面加上下面这条语句,确保以后再输入一直都是干净的: while ((c = getchar()) != EOF && c != '\n') ; 在ACM上提交的时候都用这种方法,没有出现过问题。
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。