char myChar;
string s = "penguin";
myChar = s.substr(3, 1);
The idea is for myChar to have the value of 'g' after this, but I get an error because s.substr(3, 1); is still considered a string. How can I convert that letter into a character?
Printable View
char myChar;
string s = "penguin";
myChar = s.substr(3, 1);
The idea is for myChar to have the value of 'g' after this, but I get an error because s.substr(3, 1); is still considered a string. How can I convert that letter into a character?
If you only want a single character, just use the overloaded [] operator:Quote:
Originally posted by DarkMoose
char myChar;
string s = "penguin";
myChar = s.substr(3, 1);
Code:char myChar = string("penguin")[2]; // This is the 3rd character - zero based