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

Recommended Answers

All 5 Replies

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

>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.

commented: best answer +8

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...

>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 <yadda yadda yadda>". :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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.