strcat not working

While performing strcpy on two strings

1
2
3
4
5
6
7
8
    string emp_name=m_known[win].cName;
    cout << typeid(emp_name).name() ;
    emp_name=system(emp_name.c_str());
    string s1="Emp Name : ";
    system(s1.c_str());
    system(emp_name.c_str());
    string eName=strcat(s1, emp_name)


Im getting this error:-

 
    error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char*’ for argument ‘1’ to ‘char* strcat(char*, const char*)
Last edited on
strcpy(...) works for c-strings only not for std::string. There is no need for strcpy(...) wiith std::string.
I don't know what you are trying to do on line 7. The std::string equivalent would look like:

string eName = s1 = emp_name;

By the way: system(...) does not return a string. So line 3 would lead to an invalid string.
My bad, line 7 was supposed to do strcat. Edited it
strcat(...) equivalent for std::string would be:

s1 += emp_name; // Note: +=
Hi rsingh2083,
The prototype of strcat(),you see,it's
char* strcat(char*,const char*)
so strcat() can't work with two string object.if you want the variable "eName" to be a string object,the code should be like:
string eName=s1+emp_name; //returns a string
I hope those can help you:)
Last edited on
if you want the variable "eName" to be a char* object,the code should be like:
char* eName=strcat(s1.c_str(),emp_name.c_str()); //returns a char pointer


NO.

You can't use .c_str() like this. 1) .c_str() returns a const char* not a char* and 2) you can't directly append to a std::string like this, bypassing all of the internal handling, checking, setting size etc etc etc. if you manage to get this to compile, it will blow up at run-time.
My bad.Edited it.
Topic archived. No new replies allowed.