Conversion of a number from string to vector<int>

P

Paul

Juha Nieminen said:
No, he isn't. He is trying to convert a string containing a very large
(ascii representation of an) integer into a set of ints, which is
something
atoi() won't do (nor can you easily even use it to implement the task in
question). If you tried to use atoi() for this, if the string represents
an integer larger than can fit in an int, he will get an incorrect answer.


Because it was not helpful. atoi() cannot be used to solve the problem.
If you tried to give us some actual code you would see it yourself.
I don't see any code coming from your direction.
 
P

Paul

Juha Nieminen said:
You are using doubles to handle integers? Have you ever programmed in
a C family of languages? Do you understand the inherent rounding problems
associated with floating point values? (This is especially egregious since
the problem is solvable with integers, and the solution isn't any more
complicated.)


This code was a very rough draft to ask anonymous if this is the kind of
behaviour he was looking for. Again you are fast to criticise me but you
have posted nothing yourself.
 
I

Ian Collins

No, he isn't. He is trying to convert a string containing a very large
(ascii representation of an) integer into a set of ints, which is something
atoi() won't do (nor can you easily even use it to implement the task in
question). If you tried to use atoi() for this, if the string represents
an integer larger than can fit in an int, he will get an incorrect answer.


Because it was not helpful. atoi() cannot be used to solve the problem.
If you tried to give us some actual code you would see it yourself.

Even in C, atoi() should be avoided in favour of the strtol() family.
 
P

Paul

Ian Collins said:
Even in C, atoi() should be avoided in favour of the strtol() family.
I don't know I have never used it, does it include isdigit infunction
checking and stuff.
I learned how to mask a numeric char many years ago. I have never needed to
use anything else.

I see some people are using the subtraction of char '0' technique, but this
seems a bit weird to me as chars are not supposed to behave like that..
IIRC there is something in the standard about the chars '0' - '9' having
special pirvaleges with arithmetic operators.
Does the following char arithmetic work?

'4' * '2'

Will give us an integer value of 8? Sorry too lazy to check, I m having my
morning tea :)
 
L

Lasse Reichstein Nielsen

Paul said:
I see some people are using the subtraction of char '0' technique, but
this seems a bit weird to me as chars are not supposed to behave like
that..

Like what?
The char type is an integral type. You can subtract them just fine.
IIRC there is something in the standard about the chars '0' - '9'
having special pirvaleges with arithmetic operators.

I doubt that.
Does the following char arithmetic work?

'4' * '2'

No, unless you want the result to be 2600.
Will give us an integer value of 8? Sorry too lazy to check, I m
having my morning tea :)

Not 8, no.

/L
 
P

Paul

Lasse Reichstein Nielsen said:
Like what?
The char type is an integral type. You can subtract them just fine.


I doubt that.


No, unless you want the result to be 2600.


Not 8, no.
AH ok I see it now , sorry i misunderstood.
 

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

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top