为了账号安全,请及时绑定邮箱和手机立即绑定

vc++的cctype头文件里有isdigit这个函数,是判断输入是不是数字吗?

vc++的cctype头文件里有isdigit这个函数,是判断输入是不是数字吗?

C++
精慕HU 2023-03-18 17:13:27
#include <iostream>#include <cctype>int main(){using namespace std;int ch;double tvarps;cout << "your tvarp: " << endl;while (cin >> ch && ch > 0 ){if(ch <= 5000 && ch >0)  tvarps = 0.0;else if(ch > 5000 && ch <= 15000)tvarps = ch * 0.1;  else if(ch > 15000 && ch <=35000 )tvarps = (ch - 15000)*0.15 + 1000;else if(ch > 35000)tvarps = (ch - 35000)*0.2 + 4000;cout << "tvarps: " << tvarps << endl;}return 0;}
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

在while前加char temp[200];
把while(...)改成while(cin>>temp);
循环里第一行写if (!isdigit(temp))break;
第二行写ch = atoi(temp); //把字符串变成整数

以下是我做的isdigit(),仅供参考
bool isdigit(char *buf)
{
int t = 0;
if (buf[t] == '-' || buf[t] == '+')
if (buf[1] != '\0')
t ++;
for (;buf[t] != '\0'; t ++)
if (buf[t] < '0' || buf[t] > '9')
return false;
return true;
}

查看完整回答
反对 回复 2023-03-21
  • 1 回答
  • 0 关注
  • 145 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号