佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1294|回复: 13

C++

[复制链接]
发表于 12-9-2009 08:37 PM | 显示全部楼层 |阅读模式
  1. :
  2. int x;
  3. cout<<"Enter x= ";
  4. cin>>x;
  5. cout<<x;
  6. :
复制代码
我刚学c++罢了,请问:
如果我要user只能key-in int罢了,(不要abc或小数点)
要怎样写?

希望不要像http://cforum6.cari.com.my/viewthread.php?tid=1518542&extra=page%3D1
被炸啦!

谢谢。

[ 本帖最后由 wounboshen 于 12-9-2009 11:23 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 13-9-2009 01:49 PM | 显示全部楼层
我问错问题了吗

还是我post错地方?
回复

使用道具 举报

发表于 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)吧?

除了楼上的方法,
难道我在转牛角尖?
回复

使用道具 举报

Follow Us
发表于 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();

自己找找资料
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 17-9-2009 11:15 AM | 显示全部楼层
1)请分清楚 = (assign operator)和 == (equal / compare operator)


我还不能适应,请原谅...
]
2)你无需针对每一个数字来比较,而是比对数字的范围:
if(x >= 1 && x <= 100) 这样就可以比对x的数值是否在1到100之间。

这我明白,但我希望的是当user key-in 类似2.32(不是整数时),

cout<<"Input error!";

3)是的,你在钻牛角尖,而且钻得很够力两三下(不止一下)。

针对我的问题,我只回答一次,怎样说“钻得很够力两三下(不止一下)。”



isdigit() , isalpha(), atoi();


这些不是convert的吗?
isdigit() , isalpha(), 我没看过,等我用用先..

非常谢谢你们的回帖!
希望你们继续的教导我
回复

使用道具 举报

发表于 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
  1. #include <iostream>
  2. #include <cstdlib>

  3. using namespace std;

  4. int main()
  5. {
  6.     char str = '.';
  7.     cout << isdigit(str) << endl;

  8.     str = 'a';
  9.     cout << isdigit(str) << endl;

  10.     str = '2';
  11.     cout << isdigit(str) << endl;

  12.     return 0;
  13. }
复制代码
回复

使用道具 举报

发表于 18-9-2009 12:24 PM | 显示全部楼层
现在给你一个 complete sample code,

如果你还不会的话, 那我真的是没办法了
  1. #include <iostream>
  2. #include <cstdlib>

  3. using namespace std;

  4. //function prototype
  5. bool isDigit(string str);


  6. //main
  7. int main()
  8. {
  9.     //output
  10.     //1 = true
  11.     //0 = false

  12.     //or use boolalpha if you want to

  13.     string str = "123.22";
  14.     cout << isDigit(str) << endl;

  15.     str = "abc123";
  16.     cout << isDigit(str) << endl;

  17.     str = "123456";
  18.     cout << isDigit(str) << endl;

  19.     //istring to int conversion
  20.     int x = atoi(str.c_str());

  21.     cout << "x is " << x << endl;
  22. }

  23. //digit varification
  24. bool isDigit(string str)
  25. {
  26.     for(int i = 0 ; i < str.length(); i++)
  27.         if(isdigit( str[i] ) == false )
  28.             return false;

  29.     return true;
  30. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 9-12-2025 01:59 AM , Processed in 0.145189 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表