string to char

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

Join Date: Sep 2008
Posts: 16
Reputation: san_sarangkar is an unknown quantity at this point 
Solved Threads: 0
san_sarangkar san_sarangkar is offline Offline
Newbie Poster

string to char

 
0
  #1
Oct 14th, 2008
Hello guies please help me
how can I typecast string to char
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: string to char

 
0
  #2
Oct 14th, 2008
you can't because you can't stuff a whole string into a single character. If this is about some error message you got, then post the error message along with the code you wrote.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 15
Reputation: ninwa is an unknown quantity at this point 
Solved Threads: 2
ninwa ninwa is offline Offline
Newbie Poster

Re: string to char

 
0
  #3
Oct 14th, 2008
A string cannot be cast to a char, because a char is one byte and a string can be any number of bytes. You can, however, use std::string's c_str member and create a const char* out of your string.
  1. string s = "hi";
  2. const char *cs = s.c_str();
This can be useful if you want to copy the value into a character array, which might look like this:
  1. string s ="hello";
  2. char carr[6]; // note the size, leave room for \0
  3. strcpy( carr, s.c_str() );
Or, if you want to return a single character from a string you can either use the [] operator or the at member.
  1. string s = "hello";
  2. cout << s[0]; // prints 'h'
  3. cout << s.at(1); // prints e
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: string to char

 
0
  #4
Oct 14th, 2008
Do you mean string to char* ? You could access the c_str() , which is of type const char*.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 16
Reputation: san_sarangkar is an unknown quantity at this point 
Solved Threads: 0
san_sarangkar san_sarangkar is offline Offline
Newbie Poster

Re: string to char

 
0
  #5
Oct 14th, 2008
ok , can I typecast string into double?

I want to convert string 12.12 to double 12.12
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 15
Reputation: ninwa is an unknown quantity at this point 
Solved Threads: 2
ninwa ninwa is offline Offline
Newbie Poster

Re: string to char

 
0
  #6
Oct 14th, 2008
Originally Posted by san_sarangkar View Post
ok , can I typecast string into double?

I want to convert string 12.12 to double 12.12
You can do this using stringstreams, as long as you validate the string first to make sure that it is in-fact a double value, otherwise your program will crash. An explicit cast of string to double is not supported, however.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: string to char

 
0
  #7
Oct 14th, 2008
OMG NO !!! You should NEVER typecast a string to a numeric type. Use something like strtod() to convert a char* to a double, or use stringstream, see here
Last edited by stilllearning; Oct 14th, 2008 at 9:44 pm.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC