123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|252|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> is string a number?

Sun, 23 Mar 2008, 18:43
spinal
OK, I'm trying to figure out if a string is a number (and only a number), e.g. "123" = number, "123-" = not number.
I have tried the following, but it seems not to work, ite returning 0 for everything, including proper numbers.



I'm too tired to think, but I must at least have the idea right...

Any help?

-=-=-
Check out my excellent homepage!
Sun, 23 Mar 2008, 19:02
Jayenkai
I must be tired too, 'cos it looks fine to me!
Are you sure there's no linefeeds being carried into the strings, or anything like that? (that's assuming these are file-based numbers)

-=-=-
''Load, Next List!''
Sun, 23 Mar 2008, 19:02
mike_g
strtol will convert a string to a number for you, and you can see if it was a valid number, by checking where the end pointer ends up.

https://www.cplusplus.com/reference/clibrary/cstdlib/strtol.html
Mon, 24 Mar 2008, 00:01
oscar
could it be that the numbers are outside the range defined in your if statement?
Mon, 24 Mar 2008, 01:33
spinal
it is checking each character to see if it has an ascii value between 48 and 57 (0-9).

-=-=-
Check out my excellent homepage!
Mon, 24 Mar 2008, 02:49
shroom_monk
I'd say the easiest way to debug it would be to make it show the ascii values of each character it processes (you may need to extract the function, and test it seperately). Then you could see if it was something like a linefeed, or some whitespace.

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Mon, 24 Mar 2008, 04:58
JL235
It can only handle positive integers (doesn't account for - or .) but here is my solution:

Mon, 24 Mar 2008, 05:16
Afr0
Though JL235's example probably works, remember you can also use typeof() (built-in operator in both C and C++) to check wether a part of a string is a char or an int or whatever.

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Mon, 24 Mar 2008, 06:19
JL235
I've not used typeof, but after looking around I'm pretty certain it's just a language extension and not in the C standard and is used for C preprocessing. It also wouldn't work since the the entire string is made up of chars (or at least should be) not bit's and pieces or chars and ints.

The problem is trying to convert a group of chars which represent a number into an integer. As Mike_G pointed out above you can use strtol. Example:

However if you input say '29292cats', even though that isn't a valid number it will return the 29292 as the result and so give the impression that it is ok. i.e. it'll convert what it can.

Is this what you want?
Mon, 24 Mar 2008, 07:11
spinal
guess what guys, I've just discovered atoi. Currently I dont need to check for a number '0' so this will do fine...

isNum = atoi(string);

when string is "123", it returns 123, where it is "123q" it returns 0.

That will do for now.

-=-=-
Check out my excellent homepage!
Mon, 24 Mar 2008, 07:32
Jayenkai
Hurrah!

C's insanely inexplicable set of command names eventually turns up a result in less than 24 hours!

Well done C creators!!!

For future language creators, the command should be named "VALUE" or maybe even "VAL"

-=-=-
''Load, Next List!''
Mon, 24 Mar 2008, 07:52
JL235
I completely disagree. It should be called 'toInt', 'to_i' or similar.
Mon, 24 Mar 2008, 08:34
mike_g
Firstly, I wouldent use atoi because strtol has better error handling. Its definitely worth getting in the habit of using especially if you want to parse text, as the end pointer leaves you where you left off last iteration.

The way I intended was a bit different from joes version tho:

This works for 0 too.

Edit: with a little further work you can check if the function omitted any non number characters from the end pointer position too.
Mon, 24 Mar 2008, 08:42
mike_g
"Afr0" Though JL235's example probably works, remember you can also use typeof() (built-in operator in both C and C++) to check wether a part of a string is a char or an int or whatever.

The only built in operator I know that sounds like that is sizeof which returns the container size in bytes, but would not for example differentiate between a float or an int.

Also, as for string comparisons its best to use their character value. IE:

would work for any character set,a constant value would limit it to ascii. Its also self explanatory, and saves using an ASCII chart.


Mon, 24 Mar 2008, 09:59
mike_g
Ok I thought I'd have a go at coding my own version. Its a little obfuscated, but should cover all cases:

Mon, 24 Mar 2008, 16:23
Jayenkai
I don't think Spinal's too worried about non-standard character sets!!

Remember - Keep things simple!

-=-=-
''Load, Next List!''
Mon, 24 Mar 2008, 16:48
Jayenkai
Sorry, didn't mean any offence, just that lately we've all gotten to the over-thinking stage, and it's good to sometimes sit back and just take the obvious route.

Out of interest, what exactly was wrong with Spinal's initial code, assuming he only wanted to check that a string contained "0-9"? |edit| (the DS does indeed use standard ASCII!) |edit|

-=-=-
''Load, Next List!''
Mon, 24 Mar 2008, 17:28
mike_g
From what I have read the characters 0-9 are contiguous in all character sets, A-Z is not however. Tbh I cant think of what other portability problems there would be with it. Still isdigit, isspace, isalpha, etc are not exactly hard to use. oh, and I changed my function to ignore leading/trailing whitespace.