- #include <iostream>
- #include <sstream>
- using namespace std;
- bool isNum(string str);
- int main( )
- {
- string ss1="2y5r";
- string ss2="2558";
- if(isNum(ss1))
- {
- cout<<"ss1 is a num"<<endl;
- }
- else{
- cout<<"ss1 is not a num"<<endl;
- }
- if(isNum(ss2))
- {
- cout<<"ss2 is a num"<<endl;
- }
- else{
- cout<<"ss2 is not a num"<<endl;
- }
- return 0;
- }
- bool isNum(string str)
- {
- stringstream sin(str);
- double d;
- char c;
- if(!(sin >> d))
- return false;
- if (sin >> c)
- return false;
- return true;
- }
输出结果:
ss1 is not a num
ss2 is a num