注册 登录
编程论坛 C++教室

[求助]大家好,请问怎样判断用户输入的数据是整型

youshijie 发布于 2007-08-31 02:15, 1353 次点击

比如有以下几行代码
int num,N;
cout<<"Please input the decimal number:";
cin>>num;
cout<<"And value of N:";
cin>>N;
如果输入的是4e,adg等的话就会出现错误,那我怎样判断用户输入的数据是不是整型呢,还有怎么样去掉空格,比如输入
4 5,本来我想num=45,但是结果是num=4,N=5.望不吝赐教,谢谢大家

5 回复
#2
雨中飞燕2007-08-31 02:34
用字符串接收了以后自己写代码去分析这个串



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
C/C++算法习题(OnlineJudge):[url]http://yzfy.org/[/url]
#3
HJin2007-08-31 03:16
in c++, you can check the state of input stream;

you can also use istringstream to parse the input.
#4
wingyip2007-08-31 07:01
int a;
cout<<"enter the value of a"<<endl;
if(!(cin>>a)) cout<<"error"<<endl;

應該就是醬紫吧。
#5
youshijie2007-08-31 21:53
谢谢4楼,虽然没能完全解决我的问题,但我学会了这个简单的方法.
#6
夜猫man2011-03-16 16:23
厉害啊高手
1