|
|

楼主 |
发表于 13-9-2009 01:49 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 14-9-2009 01:26 AM
|
显示全部楼层
while(b != 1){
cin >> input;
if(cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<int>::max(),'\n');
b = 0;
}
else
b = 1;
}
大概。。。。
[ 本帖最后由 24hours 于 14-9-2009 01:29 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 14-9-2009 10:38 PM
|
显示全部楼层
原帖由 wounboshen 于 12-9-2009 08:37 PM 发表 
:
int x;
coutx;
cout
首先,如果你只是想一辈子当‘编程民工’ 在低层打滚的话,你可以不用看下去了。。。
先去理解电脑如何抓取/储存你输入的字符
然后去辨别储存中的数字与其他字符有什么不同
最后根据你所要的字符特征来决定输入过滤方式 |
|
|
|
|
|
|
|
|
|
|
发表于 16-9-2009 02:20 PM
|
显示全部楼层
#4
可以给些keyword 来看看吗?
你是说 iostream 里的东西吗? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 16-9-2009 11:43 PM
|
显示全部楼层
#4
问题是我输入的范围很广,不如说1~100,
没可能我需要用:
if(x=1 || x=1 || x=2 ... || x=100)吧?
除了楼上的方法,
难道我在转牛角尖? |
|
|
|
|
|
|
|
|
|
|
发表于 17-9-2009 03:02 AM
|
显示全部楼层
你犯了几个错误:
1)请分清楚 = (assign operator)和 == (equal / compare operator)
2)你无需针对每一个数字来比较,而是比对数字的范围:
if(x >= 1 && x <= 100) 这样就可以比对x的数值是否在1到100之间。
3)是的,你在钻牛角尖,而且钻得很够力两三下(不止一下)。 |
|
|
|
|
|
|
|
|
|
|
发表于 17-9-2009 10:45 AM
|
显示全部楼层
isdigit() , isalpha(), atoi();
自己找找资料 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 17-9-2009 11:15 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 17-9-2009 10:43 PM
|
显示全部楼层
原帖由 wounboshen 于 12-9-2009 08:37 PM 发表 
我刚学c++罢了,请问:
如果我要user只能key-in int罢了,(不要abc或小数点)
要怎样写?
原帖由 wounboshen 于 17-9-2009 11:15 AM 发表 
我还不能适应,请原谅...
这我明白,但我希望的是当user key-in 类似2.32(不是整数时),
会
cout<<"Input error!";
我建议你弄清楚自己的问题才来求助,不要浪费我们的时间陪你兜圈子 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 17-9-2009 11:27 PM
|
显示全部楼层
原帖由 yeenfei 于 17-9-2009 10:43 PM 发表 
我建议你弄清楚自己的问题才来求助,不要浪费我们的时间陪你兜圈子
当user 输入整数以外的,
program要cout error字眼。
难道我的问题还不清楚吗?
还是这问题解决不到的? |
|
|
|
|
|
|
|
|
|
|
发表于 18-9-2009 12:28 AM
|
显示全部楼层
#include <iostream>
using namespace std;
int main () {
double input,floa;
int checker;
cin >> input;
checker = input;
floa = input - checker;
if(floa > 0)
cout << "not integer";
return 0;
}
不懂你要干嘛。。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 18-9-2009 12:15 PM
|
显示全部楼层
回复 11# wounboshen 的帖子
不都告诉你了?
isdigit() + atoi()
提示就是这样多
自己试式下列的 code- #include <iostream>
- #include <cstdlib>
- using namespace std;
- int main()
- {
- char str = '.';
- cout << isdigit(str) << endl;
- str = 'a';
- cout << isdigit(str) << endl;
- str = '2';
- cout << isdigit(str) << endl;
- return 0;
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 18-9-2009 12:24 PM
|
显示全部楼层
现在给你一个 complete sample code,
如果你还不会的话, 那我真的是没办法了- #include <iostream>
- #include <cstdlib>
- using namespace std;
- //function prototype
- bool isDigit(string str);
- //main
- int main()
- {
- //output
- //1 = true
- //0 = false
- //or use boolalpha if you want to
- string str = "123.22";
- cout << isDigit(str) << endl;
- str = "abc123";
- cout << isDigit(str) << endl;
- str = "123456";
- cout << isDigit(str) << endl;
- //istring to int conversion
- int x = atoi(str.c_str());
- cout << "x is " << x << endl;
- }
- //digit varification
- bool isDigit(string str)
- {
- for(int i = 0 ; i < str.length(); i++)
- if(isdigit( str[i] ) == false )
- return false;
- return true;
- }
复制代码 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|