Convert from string to char - C ++

For a program that I write based on specifications, a variable is passed to the function as a string. I need to set this string to a char variable in order to set another variable. How can i do this?

This is it in the header file:

void setDisplayChar(char displayCharToSet);

this is the function that installs it:

void Entity::setElementData(string elementName, string value){
    if(elementName == "name"){
            setName(value);
    }
    else if(elementName == "displayChar"){
    //      char c;
      //      c = value.c_str();
            setDisplayChar('x');//cant get it to convert :(
    }
    else if(elementName == "property"){
            this->properties.push_back(value);
    }
}

Thanks for the help in the advanced version!

+5
source share
2 answers

You can get a specific character from a string by simply indexing it. For example, the fifth character stris equal str[4](off by one from the first character str[0]).

Keep in mind that you will have problems if the row is shorter than your index considers.

c_str(), , char* ( C ", ), char.

, .

+8

[0], char.

char c = value[0];
+4

All Articles