How to append a char to a std::string?

后端 未结 13 2175
半阙折子戏
半阙折子戏 2020-11-29 15:58

The following fails with the error prog.cpp:5:13: error: invalid conversion from ‘char’ to ‘const char*’

int main()
{
  char d = \'d\';
  std::s         


        
13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 16:21

    I found a simple way... I needed to tack a char on to a string that was being built on the fly. I needed a char list; because I was giving the user a choice and using that choice in a switch() statement.

    I simply added another std::string Slist; and set the new string equal to the character, "list" - a, b, c or whatever the end user chooses like this:

    char list;
    std::string cmd, state[], Slist;
    Slist = list; //set this string to the chosen char;
    cmd = Slist + state[x] + "whatever";
    system(cmd.c_str());
    

    Complexity may be cool but simplicity is cooler. IMHO

提交回复
热议问题