I've been reading some documentation about strtol here
and in the example a guy uses space to show this funtion when a new number begins. I'm new to C++ and I don't know much abount pointers and how they work yet, so I decided yo ask you about it. Can I use a dot instead of space to split numbers? For example, if I have this: char text[] = "3.16.88"
and I want to convert it to three diffrent variables like this a = 3, b = 16, c = 88
, can I still use strtol or should I try something else?
Thanks
Asked
Active
Viewed 402 times
0
-
Have a look at strtok function. – zdf Mar 13 '16 at 14:01
-
I'm thinking you could just try it a lot faster than posting that question here and waiting for us to answer it for you. In general, we discourage questions that show the OP did not do any research before asking. A much better question would be when using strtol(), the man page says the input will stop when any non digit is encountered and the non digit is left in the input stream. is `.` considered a digit.? Even then, you could have written a few lines of code to parse your posted string to determine if strtol() works like you hope it does a lot faster than posting the question. – user3629249 Mar 15 '16 at 03:11
1 Answers
4
That's exactly what the documentation says.
In the case of decimals, only digits 0..9
will get parsed, and the scanning will stop at the next non-digit.
Leading whitespace will automatically be discarded. If you want to scan ahead for the next number, you need to add a simple loop to skip non-digits, starting at the end_ptr
that strtol
can return in one of its parameters (use it; don't set it to NULL
).

Jongware
- 22,200
- 8
- 54
- 100
-
2`strtol` doesn't return the end pointer. It returns the result of the parse. – Kerrek SB Mar 13 '16 at 13:10
-
I'm really puzzled with pointers, can you please make this code work: `char input[MY_SIZE]; std::cin.getline(input, MY_SIZE, '\n');`. I need the user to enter something like 7.21.88 then convert these numbers into ints and assign them to different variables. – Michael Mar 13 '16 at 13:29
-