c++stringcharerror-correction

Cannot convert string to char in c++?


Does anybody know what's wrong with this code?

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
string a;
getline(cin, a);
for(;;)
{
    string x;
    x=1;
    string b;
    getline(cin, b);
    string c;
    getline(cin, c);
    string d;
    d=a+b;
    string e;
    e=b+c;
    if(b=="1")
    {
        return 0;
    }
    rename(d, e);
}
}

It says that the error is on the

rename(d,e);

part. And it gives an error

cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const char*' for argument '1' to 'int rename(const char*, const char*)

So I'm assuming it can't convert string to char. Does anybody know how to do it and send the corrected part?


Solution

  • There is no implicit conversion from std::string to char pointer, you need to invoke it via a function call:

    rename( d.c_str(), e.c_str() );