If the string is all numbers

Started by
19 comments, last by Tradone 17 years, 11 months ago
Quote:Original post by Anonymous Poster
Also keep in mind that if a string that has "-123.567" is a number you will need to check if "-" and "." are allowed. I do not know if you are interested in the punctuation of a number, but I will leave that up to you.


You have a point here. Of course, testing if a string represents any kind of number can be tricky ("+-+123.344e008" is a number too). If you want to be able to spot all the different kind of numbers, I'm afraid you'll have to use a regular expression.

Regards,
Advertisement
bool isStringNumber(char *str, int len){
bool returnResult = TRUE;

if((str[0] > '0') && (str[0] < '9')){
if(len > 0)
returnResult = isStringNumber(&(char[1]), len-1);
}else
returnResult = FALSE;


return returnResult;
}
Quote:Original post by rdansie
bool isStringNumber(char *str, int len){
bool returnResult = TRUE;

if((str[0] > '0') && (str[0] < '9')){
if(len > 0)
returnResult = isStringNumber(&(char[1]), len-1);
}else
returnResult = FALSE;


return returnResult;
}
If you're specifically offering a C solution, then great, I suppose (although your function has a couple of errors in it and really doesn't need to be recursive).

However (and this is editorializing a bit) I don't quite understand why people insist on posting hand-rolled, untested, unsafe, and/or non-standard solutions to threads like this after the (arguably) correct solution has already been given.

Read the thread. The test can be performed in one line using boost, and if you don't like that Zahlman posted a nice alternative using functors and the standard library. If you're seriously proposing the above or something like it as a solution, I think you need to provide some supporting arguments as to why it's superior to what's already been suggested.

Anyway, I don't mean to be negative; really my motivation is just to clarify to those who might read this thread (and might not have the experience to sort the wheat from the chaff, as it were) that standardized solutions are generally to be preferred.
Quote:Original post by jyk
However (and this is editorializing a bit) I don't quite understand why people insist on posting hand-rolled, untested, unsafe, and/or non-standard solutions to threads like this after the (arguably) correct solution has already been given.

Read the thread. The test can be performed in one line using boost, and if you don't like that Zahlman posted a nice alternative using functors and the standard library.


Well, it *is* good to know the (written very specific to the problem) algorithms in case you ever find yourself in an environment (language, target device, etc. etc.) that doesn't provide support for what you want to do. But then, only really experienced programmers ought to be working in those environments - people who are capable of coming up with these things themselves, and getting them right (able to give a proof, and also have it pass tests) the first time (or at least within the first few times).

Sadly, the world doesn't always work that way ;(
Quote:Original post by Emmanuel Deloget
Quote:Original post by Anonymous Poster
Also keep in mind that if a string that has "-123.567" is a number you will need to check if "-" and "." are allowed. I do not know if you are interested in the punctuation of a number, but I will leave that up to you.


You have a point here. Of course, testing if a string represents any kind of number can be tricky ("+-+123.344e008" is a number too).

That's technically an expression, not a numeric literal. (But the result of the expression is the literal "-123.344e008".)
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
The topic is called "if the string is all numbers", so I assume that there are really only digits in the string, no sign, radix character or any other non-digit character.

But if they're allowed there is a really easy and lame way:
double strAsDouble = atof( theString );if( strAsDouble != 0.0 ) //a double can store a natural number without loss of precision, so comparison is possible


The only thing I'm wondering is that atof is returning 0.0, which can also be a valid result of converting a string to a number (according to http://www.cplusplus.com/ref/cstdlib/atof.html). That's really strange, that in the standard, there is a function with such errorneous error handling.
What is the header file for `boost::algorithm'?
Sorry, I know this is a dumb question, but I'm not getting any matches..
Quote:Original post by Tradone
What is the header file for `boost::algorithm'?
Sorry, I know this is a dumb question, but I'm not getting any matches..


You need Boost to use Boost::algorithm.
Yea, I have the boost library installed.
I'm using their serialization class to serialize my map files, and also use their lexical cast :D

Do you know the header files that I need to include for boost::algorithms::all isdigit() ??
boost/algorithm/string.hpp

:-)

This topic is closed to new replies.

Advertisement