高手指教:c++ I/O流类库

MMNG 2003-01-11 01:13:28
# include <iostream.h>
main()
{
int ch=0;
cin>>ch;
while (ch<1||ch>5)
{
cout<<"请重新输入:"<<endl;
cin>>ch;
}
cout<<ch<<endl;
return true;
}
在运行过程中,如果输入一个字符(并按下回车键后),将发生死循环.
望高手指教.
...全文
30 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cybergate 2003-01-23
  • 打赏
  • 举报
回复
我想cin的>>和scanf的功能类似的.它们都要从缓冲区取字符,对于你的例子,如果读入的字符不是数字,它将把这个字符送回缓冲区,然后退出函数.那么你输入的这个字母就会不停地被读出来又被放回去--相当于缓冲区没有进行任何取操作,你说会不会死机?
Cybergate 2003-01-23
  • 打赏
  • 举报
回复
挖vc代码的那位说得对。

楼主说死循环的意思是:输入一个字符后,进入死循环,这个死循环中的cin>>ch语句是不会等待输入的,所以看上去像死机了一样
NetBird_China 2003-01-16
  • 打赏
  • 举报
回复
没这么复杂吧,从键盘敲入的字符的ASCII都大于你程序中的值,当然是死循环了
Firstbyte 2003-01-16
  • 打赏
  • 举报
回复
废话,一个字符的ASCII码肯定大于5嘛!!!
chinajiji 2003-01-16
  • 打赏
  • 举报
回复
你这个问题讨论过,见:
http://expert.csdn.net/Expert/topic/1334/1334744.xml?temp=.1651422
coolblue 2003-01-16
  • 打赏
  • 举报
回复
在cin >> ch后加上
if (!cin)判断,
wlfjck 2003-01-11
  • 打赏
  • 举报
回复
以下代码是从vc6中拷贝出来的

istream& istream::operator>>(int& n)
{
_WINSTATIC char ibuffer[MAXLONGSIZ];
long value;
char ** endptr = (char**)NULL;
if (ipfx(0))
{
value = strtol(ibuffer, endptr, getint(ibuffer));
if (value>INT_MAX)
{
n = INT_MAX;
state |= ios::failbit;
}
else if (value<INT_MIN)
{
n = INT_MIN;
state |= ios::failbit;
}
else
n = (int) value;

isfx();
}
return *this;
}

当调用cin<<ch时,程序会调用以上程序,注意value = strtol(ibuffer, endptr, getint(ibuffer))这行语句中getint(ibuffer)会调用如下程序
int istream::getint(char * buffer) // returns length
{
int base, i;
int c;
int fDigit = 0;
int bindex = 1;

if (x_flags & ios::dec)
base = 10;
else if (x_flags & ios::hex)
base = 16;
else if (x_flags & ios::oct)
base = 8;
else
base = 0;

if (ipfx(0))
{
c=bp->sgetc();
for (i = 0; i<MAXLONGSIZ-1; buffer[i] = (char)c,c=bp->snextc(),i++)
{
if (c==EOF)
{
state |= ios::eofbit;
break;
}
if (!i)
{
if ((c=='-') || (c=='+'))
{
bindex++;
continue;
}
}
if ((i==bindex) && (buffer[i-1]=='0'))
{
if (((c=='x') || (c=='X')) && ((base==0) || (base==16)))
{
base = 16; // simplifies matters
fDigit = 0;
continue;
}
else if (base==0)
{
base = 8;
}
}


// now simply look for a digit and set fDigit if found else break

if (base==16)
{
if (!isxdigit(c))
break;
}
else if ((!isdigit(c)) || ((base==8) && (c>'7')))
break;

fDigit++;
}
if (!fDigit)
{
state |= ios::failbit;
while (i--)
{
if(bp->sputbackc(buffer[i])==EOF)
{
state |= ios::badbit;
break;
}
else
state &= ~(ios::eofbit);
}
i=0;
}
// buffer contains a valid number or '\0'
buffer[i] = '\0';
isfx();
}
if (i==MAXLONGSIZ)
{
state |= ios::failbit;
}
return base;
}

这段语句的功能大致是从istream中读取一个int,如果失败的话,将读取的东西再放回到istream中,同时设置ios失败的标志位。

所以,如果输入失败的是字符的话,就会失败,所以会死循环了。

可以用cin.fail()来判断读取是否失败

#include "stdafx.h"

# include <iostream.h>
main()
{
int ch=0;
cin>>ch;

if(cin.fail())
{
cout<<"failed"<<endl;
cin.flush();
}



#if 0
while (ch<1||ch>5)
{

cout<<"请重新输入:"<<endl;
cin>>ch;
}
cout<<ch<<endl;
#endif
return true;
}



wlfjck 2003-01-11
  • 打赏
  • 举报
回复
不好意思,我的回答错了,应该是cin读取整形是因为是字符,所以失败,ch的值还是0,所以一直死循环了。

修改的例子如下

# include <iostream.h>
main()
{
int ch=0;
cin>>ch;
cout<<ch<<endl;
while (ch<1||ch>5)
{
cout<<ch<<endl;
cout<<"请重新输入:"<<endl;
cin>>ch;
cout<<ch<<endl;
}
cout<<ch<<endl;
return true;
}
foxmail 2003-01-11
  • 打赏
  • 举报
回复
回车=13
所以它》5,满足,一直循环
wlfjck 2003-01-11
  • 打赏
  • 举报
回复
操作符优先级问题,修改过后的程序。

# include <iostream.h>
main()
{
int ch=0;
cin>>ch;
while ( (ch<1) || (ch>5) )
{
cout<<"请重新输入:"<<endl;
cin>>ch;
}
cout<<ch<<endl;
return true;
}
Hanyu94118 2003-01-11
  • 打赏
  • 举报
回复
大哥你CH定的是INT型你不可能给它付予它CHAR型

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧