the easy way to check if the string is a number or not?

K

kathy

I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...

what is the easy way for only interger?
 
D

Dietmar Kuehl

kathy said:
I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...

Try to read it using a 'std::istringstream'. If this is not what you
want, check out Boost's regular expression library and provide a
regular expression describing the formats you want.
 
R

Roland Pibinger

I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...

check out strtod:
http://www.opengroup.org/onlinepubs/000095399/functions/strtod.html
what is the easy way for only interger?

strtol

In both cases you need to understand what the 'endptr' does. Hint:
strip trailing whitespace before calling strtod or strtol. Encapsulate
the check in a reusable function.

Best wishes,
Roland Pibinger
 
B

BobR

kathy wrote in message
I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...
what is the easy way for only interger?

If the others did not give you the answer you wanted, maybe this will help
you.

// ----------------------------------------------
// #include <ccytpe> // ISO C++ 14882: std::isdigit()
std::cout<<"\n_______ isdigit test _______"<<std::endl;
int Inum(0);
double Dnum(0);
std::string TestForNum("Hi 4 76.5num 42.7e-5");
std::cout<<"Test string = "<<TestForNum<<"."<<std::endl;
for(size_t i(0); i < TestForNum.size(); ++i){
std::cout<<"char at "<<i<<" is "<<TestForNum.at(i)<<", a ";
if( std::isdigit( TestForNum.at(i) ) ){
std::istringstream strStream( TestForNum.substr(i) );
strStream >> Inum;
strStream.clear();
strStream.str( TestForNum.substr(i) );
strStream >> Dnum;
strStream.clear();
std::cout<<"digit Inum="<<Inum
<<" Dnum="<<Dnum<<std::endl;
}
else{ std::cout<<"alpha"<<std::endl;}
}
std::cout<<"_______ isdigit test _______Done"<<std::endl;
/* -- output --
_______ isdigit test _______
Test string = Hi 4 76.5num 42.7e-5.
char at 0 is H, a alpha
char at 1 is i, a alpha
char at 2 is , a alpha
char at 3 is 4, a digit Inum=4 Dnum=4
char at 4 is , a alpha
char at 5 is 7, a digit Inum=76 Dnum=76.5
char at 6 is 6, a digit Inum=6 Dnum=6.5
char at 7 is ., a alpha
char at 8 is 5, a digit Inum=5 Dnum=5
char at 9 is n, a alpha
char at 10 is u, a alpha
char at 11 is m, a alpha
char at 12 is , a alpha
char at 13 is 4, a digit Inum=42 Dnum=0.000427
char at 14 is 2, a digit Inum=2 Dnum=2.7e-005
char at 15 is ., a alpha
char at 16 is 7, a digit Inum=7 Dnum=7e-005
char at 17 is e, a alpha
char at 18 is -, a alpha
char at 19 is 5, a digit Inum=5 Dnum=5
_______ isdigit test _______Done
*/
// ----------------------------------------------

Open <ccytpe> in an editor for a list of more useful items.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top