943,664 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2339
  • C++ RSS
Oct 14th, 2008
0

string to char

Expand Post »
Hello guies please help me
how can I typecast string to char
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
san_sarangkar is offline Offline
16 posts
since Sep 2008
Oct 14th, 2008
0

Re: string to char

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Oct 14th, 2008
0

Re: string to char

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.
c++ Syntax (Toggle Plain Text)
  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:
c++ Syntax (Toggle Plain Text)
  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.
c++ Syntax (Toggle Plain Text)
  1. string s = "hello";
  2. cout << s[0]; // prints 'h'
  3. cout << s.at(1); // prints e
Reputation Points: 10
Solved Threads: 2
Newbie Poster
ninwa is offline Offline
15 posts
since Sep 2008
Oct 14th, 2008
0

Re: string to char

Do you mean string to char* ? You could access the c_str() , which is of type const char*.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 14th, 2008
0

Re: string to char

ok , can I typecast string into double?

I want to convert string 12.12 to double 12.12
Reputation Points: 10
Solved Threads: 0
Newbie Poster
san_sarangkar is offline Offline
16 posts
since Sep 2008
Oct 14th, 2008
0

Re: string to char

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
ninwa is offline Offline
15 posts
since Sep 2008
Oct 14th, 2008
0

Re: string to char

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help plz! C++ code help...
Next Thread in C++ Forum Timeline: Can someone run my code template





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC