0

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?

8
  • 4
    rename(d.c_str(), e.c_str()); Commented Mar 1, 2020 at 20:43
  • Thanks, why not post this as an answer too so I can give you an upvote. Commented Mar 1, 2020 at 20:45
  • 1
    You should have posted what rename does. All that can be seen in your post is the signature of rename. what @Sebastian Hoffmann says suits the signature, but does it produce the desired effect? Commented Mar 1, 2020 at 20:50
  • 1
    @lakeweb It's the rename from the C standard library (stdio.h) Commented Mar 1, 2020 at 21:10
  • 1
    @lakeweb it's std::rename , and askers don't need to post details of standard functions. You could look up documentation if unfamiliar with the function Commented Mar 1, 2020 at 21:18

1 Answer 1

2

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() ); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.