question about string element

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

question about string element

 
0
  #1
Oct 8th, 2009
I got a question about c++ strings.

Let's say string text = "0R14"

and I access each element using text[0],text[1] etc

does text[0] return an integer 0? or a character '0'?

if it doesn't return an integer, how do I convert individual string element to integer type if I need to do integer calculations?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 35
Reputation: NeoKyrgyz is an unknown quantity at this point 
Solved Threads: 1
NeoKyrgyz NeoKyrgyz is offline Offline
Light Poster
 
0
  #2
Oct 8th, 2009
atoi("0");
(int ) text[0]-48
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 8th, 2009
will atoi(text[0]) work too?
because doing atoi("0") is hardcoding....
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
3
  #4
Oct 8th, 2009
atoi(text[0]) will not work because atoi expects a char*, not a char. That half of NeoKyrgyz's answer was basically useless and dumb.

If you want to convert the character "c" representing the decimal digit n to the integer n, you can do so with (c - '0') . That's the same as what NeoKyrgyz said, because '0' is just a fancy way of writing 48 . You'll note that '0' == 48, '1' == 49, ..., '9' == 57. If text is a string containing "0R14", then text[0] will evaluate to 48.

atoi is for converting c-style strings containing sequences of digits into numbers, e.g. "123" -> 123
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training
 
0
  #5
Oct 8th, 2009
thanks alot for explaining!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC