Convertings strings to numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2007
Posts: 2
Reputation: ledox is an unknown quantity at this point 
Solved Threads: 0
ledox ledox is offline Offline
Newbie Poster

Re: how to convert char to int

 
0
  #1
Jun 1st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 17
Reputation: blazted is an unknown quantity at this point 
Solved Threads: 0
blazted blazted is offline Offline
Newbie Poster

Re: Convertings strings to numbers

 
0
  #2
Jun 1st, 2007
You can use atoi to convert the char to a int or atof to a double
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Convertings strings to numbers

 
1
  #3
Jun 1st, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2
Reputation: ledox is an unknown quantity at this point 
Solved Threads: 0
ledox ledox is offline Offline
Newbie Poster

Re: Convertings strings to numbers

 
0
  #4
Jun 3rd, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,789
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 746
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Convertings strings to numbers

 
0
  #5
Jun 3rd, 2007
>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>". 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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 17
Reputation: blazted is an unknown quantity at this point 
Solved Threads: 0
blazted blazted is offline Offline
Newbie Poster

Re: Convertings strings to numbers

 
0
  #6
Jun 4th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC