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

#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;

}

精慕HU
浏览 147回答 1
1回答

潇潇雨雨

在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;}
打开App,查看更多内容
随时随地看视频慕课网APP