scanf行为 [英] scanf behaviour

查看:93
本文介绍了scanf行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


我可能会重新发明...

我试图在scanf的帮助下读取正整数,如果

输入值不是正数,而是负零或char,i
必须重新读取输入,直到得到所需的pos。数字


我编写了代码,但它的工作方式并没有:如果我把

一些字符输入输入,程序无限循环而不是

让我输入一个新值。


请详细说明问题所在。谢谢

Hello.

I maybe reinvent the weel ...
I''m trying to read positive integer number with the help of scanf, if
the input value is not positive number but negaive one zero or char , i
have to reread the input until I get the needed pos. number

I wrote the code , but it doesn''t work the way it should : if i put
some char into input, the program goes infinite loop instead of
promting me to enter a new value.

Please shed some light on what is the problem. thanks

展开 | 选择 | Wrap | 行号

推荐答案

st ****** @ gmail.com 写道:

你好。


我可能会重新发明... ...

我试图在scanf的帮助下读取正整数,如果

输入值不是正数但是否定一个零或字符,我必须重新读取输入,直到我得到所需的位置。数字


我编写了代码,但它的工作方式并没有:如果我把

一些字符输入输入,程序无限循环而不是

让我输入一个新值。
Hello.

I maybe reinvent the weel ...
I''m trying to read positive integer number with the help of scanf, if
the input value is not positive number but negaive one zero or char , i
have to reread the input until I get the needed pos. number

I wrote the code , but it doesn''t work the way it should : if i put
some char into input, the program goes infinite loop instead of
promting me to enter a new value.



这是因为转换失败并且字符保留在

stream。所以下次调用scanf时,相同的字符序列是

读取。


添加


fflush( stdin);

你的printf之后的

That''s because the conversion fails and the characters remain in the
stream. So the next time scanf is called, the same character sequence is
read.

Add

fflush( stdin );

after your printf.


请详细说明问题所在。谢谢
Please shed some light on what is the problem. thanks
展开 | 选择 | Wrap | 行号


Ian Collins说:
Ian Collins said:

Add


fflush(stdin);
Add

fflush( stdin );



不,不要那样做。 fflush打开输入的流的行为是

undefined。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

No, don''t do that. The behaviour of fflush on streams open for input is
undefined.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Richard Heathfield写道:
Richard Heathfield wrote:

Ian Collins说:

Ian Collins said:


>>添加

fflush(stdin);
>>Add

fflush( stdin );




不,不要那样做。 fflush打开输入流的行为是

undefined。



No, don''t do that. The behaviour of fflush on streams open for input is
undefined.



糟糕,所以它是。


我一直使用Solaris手册页定义:


"如果流指向输入流或更新流进入

最近的操作是输入的,如果它是可搜索的并且还没有在文件结尾处,则该流是刷新的。


- -

Ian Collins。

Oops, so it is.

I''ve always used the Solaris man page definition:

"If stream points to an input stream or an update stream into
which the most recent operation was input, that stream is
flushed if it is seekable and is not already at end-of-file."

--
Ian Collins.


这篇关于scanf行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆