c++判断输入的是否为数字?

ycwan 2009-08-05 11:12:36
c++判断输入的是否为数字?


请给出详细的代码..
要求如下.
我要用户输入一个数字.
所以要判断用户输入的是否为数字.
输入的不是数字要给出响应..
谢谢了...
...全文
9809 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
SEUU 2009-08-06
  • 打赏
  • 举报
回复
太懒了吧,这么easy的东东也不动手自己弄。
e21124215 2009-08-06
  • 打赏
  • 举报
回复
int GetInt(int nNum)
{

while(1)
{
char szBuf[255] = {'\0'};
cin>>szBuf;
nNum = AtoI(szBuf);
if (-1 == nNum)
{
cout<<"输入了非法字符串,请输入整数"<<endl;
}
else
{
break;
}
}

return nNum;
}

int AtoI(const char *pStr)
{
int nI = 0;
int nJ = 1;
int nA = 0;

for (;nI < StrLen(pStr);nI++)
{
if ('0' > pStr[nI] || '9' < pStr[nI])
{
return -1;
}
}
for (int nK = StrLen(pStr) - 1;nK >= 0;nK--)
{
nA += (pStr[nK] - '0') * nJ;
nJ *= 10;
}

return nA;
}

int StrLen(const char *pStr)
{
int nI = 0;
while ('\0' != pStr[nI])
{
nI++;
}
return nI;
}


这个可以吗?可以用来输入一个整数并不会出现死循环
cyxcw1 2009-08-06
  • 打赏
  • 举报
回复
三楼的已经可以了的,利用了输入流的异常!
用CCTYPE那个也可以!
bestyoung1 2009-08-06
  • 打赏
  • 举报
回复 2
用万能类型variant存用户输入的数
variant v1;//定义个V1变量,之后让用户往里存东西
if(v1.vt==VT_I4)//判断V1的类型,VT_I4是4位的INT,如果是数字的话,执行

else if(v1.vt==BT_BSTR) //如果是字符串的话,执行
student008 2009-08-06
  • 打赏
  • 举报
回复
两种方法
1:用ctype.h里的isdigit(x);
2; 逐一判断各个数字德ascll码
char ch;
ch=getchar();
if(ch>=48&&ch<=57)
cout<<"is digit"<<endl;
else
cout<<"is not digit"<<endl;
zhuweiping2003 2009-08-06
  • 打赏
  • 举报
回复
mark UP
26°C东皇大叔 2009-08-06
  • 打赏
  • 举报
回复
输入除错就刷新流重新输入.
abcdef0966 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ycwan 的回复:]
引用 1 楼 hairetz 的回复:
int num = 0;
cin>>num;
....        //跳到这里的都不是数字,简单不?


看不懂  ...
[/Quote]

如果输入不是int型,流就会出错
mengjfu 2009-08-06
  • 打赏
  • 举报
回复
#include <stdio.h> 
#include <stdlib.h>

int main()
{
int i;
printf("please input a num:");
scanf("%d",&i);
if(i>=0&&i<=9)
printf("\nthe num is:%d",i);
else
printf("\nerror");

}
szqh97 2009-08-06
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
int main()
{
int num;
if(cin>>num)
cout<<"Input correct!"<<endl;
else
cout<<"Input erroe!"<<endl;
return 0;
}

你看这样行不?
ysysbaobei 2009-08-06
  • 打赏
  • 举报
回复
顶下
十八道胡同 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hairetz 的回复:]
int num = 0;
cin>>num;
....        //跳到这里的都不是数字,简单不?
[/Quote]

利用输入流来判断。。
lgw26046044 2009-08-06
  • 打赏
  • 举报
回复
#include <cctype>
#include<string>
#include<iostream>
using namespace std;
int main(void)
{
string str;
bool shuzi;
do
{
shuzi=true;
cout<<"请输入数字:"<<endl;
cin>>str;

for(string::iterator iter=str.begin();iter!=str.end();iter++)
{
if(!isdigit(*iter))
{
shuzi=false;
cout<<"输入含有非数字字符,请从新输入。"<<endl;
break;
}

}

}while(!shuzi);
system("pause");
return 0;

}


kdy88120 2009-08-06
  • 打赏
  • 举报
回复
这么简单的问题。。。
判断ASCII码就行了
风老二 2009-08-06
  • 打赏
  • 举报
回复

int num;

while(!(cin>>num)){ //cin输入错误时执行下边语句
cin.clear(); //清除流标记
cin.sync(); //清空流
cout<<"输入了非数字字符"<<endl; //打印错误提示
}
IPAS_Gehool 2009-08-06
  • 打赏
  • 举报
回复
按照2楼说的,通过异常处理就行了
lzh9955 2009-08-05
  • 打赏
  • 举报
回复
GOOD!
ycwan 2009-08-05
  • 打赏
  • 举报
回复
3楼的 可以.. 谢谢了..
ycwan 2009-08-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 padiu01 的回复:]
C++中有自带的方法isdigit(c)
[/Quote]

这个函数 我不太会用.. 因为每次调用 都会显示 是数字..
adventurelw 2009-08-05
  • 打赏
  • 举报
回复
#include <cctype>

if(!isdigit(x))
......
加载更多回复(4)

65,196

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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