C++判斷string是不是數字
阿新 • • 發佈:2019-02-04
- #include <iostream>
- #include <sstream>
- usingnamespace 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"
- }
- 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))
- returnfalse;
- if (sin >> c)
- returnfalse;
- returntrue;
- }
輸出結果:
ss1 is not a num
ss2 is a num