| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1791 人关注过本帖
标题:本人发现c语言中关于scanf()函数一个怪异的问题?
只看楼主 加入收藏
钢燃
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2005-5-19
收藏
得分:0 
以下是引用cosixu在2005-5-25 5:10:48的发言: scanf("%c",&temp); file://第一次输入一个数据的时候其实是两个字符存入缓冲区了 比如你输入"q"然后回车,那么字符q和回车就留大缓冲区 解决的办法是加一个变量char ch; 然后在第一次scanf("%c",&temp);后面加一句ch=getchar();问题就解决了
不懂,你的意思是:S中第一次存的是“回车”?

寻找同济的朋友的网络连接
2005-05-25 15:39
Fangel
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-5-16
收藏
得分:0 
你只要在%c的前面加上空格就可以了,scanf(" %c",&temp);  
2005-05-28 22:57
Antigloss
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2004-12-30
收藏
得分:0 
以下是引用天使预备役在2005-5-24 11:56:55的发言: 错了,执行了,但因为你的键盘缓冲区里有东西,所以他直接就读他了,而不在等你输入! 在想要让计算机等待你敲键盘的地方之前清一下键盘缓冲区就可以了:fflush(stdin);(知道加哪吧?)!

fflush(stdin) is wrong On occasions you may need to clear unwanted data in an input stream, most

commonly keyboard input. One frequently suggested way of doing this is by using

fflush(stdin). This is incorrect, and should be avoided, here is why.

stdin is a standard FILE* variable that points to the input stream normally used

for keyboard input. The fflush() function is deemed to flush buffers. Put the two

together and you have a method for clearing the input stream easily, right?

WRONG! This is a common misconception in C and C++ programming, an extract from

the C standard will help explain:

int fflush(FILE *ostream);

ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

So, if the file stream is for input use, as stdin is, the behaviour is undefined,

therefore it is not acceptable to use fflush() for clearing keyboard input. As usual, there are some exceptions, check your compiler's documentation to see

if it has a (non-portable) method for flushing input.

2005-05-31 13:16
Antigloss
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2004-12-30
收藏
得分:0 

On occasions you may need to clear unwanted data in an input stream, most

commonly keyboard input. This may be after a call to a read function that failed

to input all available data, or it may be to ensure that the user doesn't try the

"type ahead" approach when using your application.

As far as standard C and C++ go, there is no guaranteed method to clear an input

stream. You can write code that will do a reasonably good job, but it probably

won't work in all instances your require it to. Why not? Because in standard

C/C++ input streams are buffered. This means that when you hit a key on the

keyboard, it isn't sent directly to your program. Instead it is buffered by the

operating system until such time that it is given to your program. The most

common event for triggering this input is the pressing of the [Enter] key.

If you are sure that unwanted data is in the input stream, you can use some of

the following code snippets to remove them. However, if you call these when there

is no data in the input stream, the program will wait until there is, which gives

you undesirable results.

/* * This C implementation will clear the input buffer. * The chances are that the buffer will already be empty, * so the program will wait until you press [Enter]. */

#include <stdio.h>

int main(void) { int ch; char buf[BUFSIZ]; puts("Flushing input"); while ((ch = getchar()) != '\n' && ch != EOF); printf ("Enter some text: "); if (fgets(buf, sizeof(buf), stdin)) { printf ("You entered: %s", buf); } return 0; }

/* * Program output: * Flushing input blah blah blah blah Enter some text: hello there You entered: hello there * */

And a C++ version:

#include <iostream> #include <cstdio>

using std::cin; using std::cout; using std::endl;

int main(void) { int ch; char buf[BUFSIZ]; cout <<"Flushing input" <<endl; while ((ch = cin.get()) != '\n' && ch != EOF); cout <<"Enter some text: "; cout.flush(); if (cin.getline(buf, sizeof(buf))) { cout <<"You entered: " <<buf <<endl; }

return 0; }

/* * Program output: * Flushing input blah blah blah blah Enter some text: hello there You entered: hello there * */

But what about fflush(stdin); Some people suggest that you use fflush(stdin) to clear unwanted data from an

input buffer. This is incorrect.

2005-05-31 13:17
钢燃
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2005-5-19
收藏
得分:0 
Antigloss 你学的很正统,我向拜你为师。 问一下: while ((ch = cin.get()) != '\n' && ch != EOF); 中的ch!=EOF是什么意思呀? EOF是什么类型的?不是函数吧?他的值是什么? 还有什么时候ch==EOF?

寻找同济的朋友的网络连接
2005-06-04 10:01
牛虻
Rank: 1
等 级:新手上路
威 望:1
帖 子:472
专家分:0
注 册:2004-10-1
收藏
得分:0 
以下是引用钢燃在2005-6-4 10:01:16的发言: Antigloss 你学的很正统,我向拜你为师。 问一下: while ((ch = cin.get()) != '\n' && ch != EOF); 中的ch!=EOF是什么意思呀? EOF是什么类型的?不是函数吧?他的值是什么? 还有什么时候ch==EOF?
EOF和NULL一样,是一个TC里面默认的值,用来判断文件操作里面读取结束的判断符

土冒
2005-06-04 12:29
牛虻
Rank: 1
等 级:新手上路
威 望:1
帖 子:472
专家分:0
注 册:2004-10-1
收藏
得分:0 
antigloss发的文章真是好极了,非常细致啊。
看来真得去买点老外的精品来看看了

土冒
2005-06-04 12:37
lullaby
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2005-4-22
收藏
得分:0 
学到了一些东西
非常感谢
2005-06-04 22:36
musheng_cn
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2004-12-2
收藏
得分:0 
在C中叫垃圾字符,就是指在前一次输入的时候你所输入的一个字符和一个回车,其中那个回车就叫垃圾字符.在接着输入时必须处理掉.楼上的提的解决方案是可行的!
2005-06-07 20:09
淘淘
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2005-5-18
收藏
得分:0 
int main(){return 0;} 
这句就是清空输入缓存程序
直接加到程序上面就可以的对吗 
 

天上会闪的是什么啊? 我要把你摘下来.     ..................放到家里的窗前
2005-06-07 22:17
快速回复:本人发现c语言中关于scanf()函数一个怪异的问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016293 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved