954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Convertings strings to numbers

hi, i have a another question about converting char to int in C++.
if i have char n="123" and char n2= "87" and i wanna sum those two char...how can i do that? i know i have to change each char in int but for the rest...
thank

ledox
Newbie Poster
2 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

You can use atoi to convert the char to a int or atof to a double

blazted
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

>You can use atoi to convert the char to a int
Or not, seeing as how atoi is a demon spawn. Use strtol instead, or since this is C++, stringstream or Boost::lexical_cast. Examples are common and can be found with this forum's search feature.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

i tried the function atoi() and i think it only works for char and not for string.
what i'm trying to do, its to ask the user to enter two numerics number in string type and add those two strings and the result must be a integer.

i tried the function _toascii()-48...

ledox
Newbie Poster
2 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

>i tried the function atoi() and i think it only works for char and not for string.
Well there's your problem. You're guessing. "I think it only works for ". :icon_rolleyes: It works perfectly for string types, assuming you know what a string type is and which one atoi accepts, except for the fact that atoi is horribly BROKEN! I mean, really. Do you people try to pick the worst possible option out of any list of choices I offer?

>i tried the function _toascii()-48...
... Yea, maybe you should post your code so that someone clueful can help you make it right.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Well you did ask for char to double. I agree atoi sucks but he was asking for something simple. If you are trying to do a string to int or double do a simple search or post the code.


For Atof convert to c_str

blazted
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You