Prompting the user for a filename.

I guess I'm not even sure if you can do this, but I have no idea why you couldn't. I am trying to use a string variable as the name of a file, but to no avail. My code will not even compile, and I've no idea why.

{ofstream output;
string filename=" ";
cout<<"Specify the name you would like to save the file as with the extension .txt. ";
cin>>filename;
output.open(filename,ios::out);
}

I've also tried getline(cin, filename); instead, but they both give me the same error:

"no matching function for call."

Maybe there's a glaring error that I'm just missing. Thanks! :)
There is no open() function that has a string as the first argument.

Check your documentation.

That function has a const char* as the argument for the file name.

Therefore:

output.open(filename.c_str(),ios::out);

and off you go.

Gosh, thank you so much. Now I feel pretty forgetful. I really appreciate it. :)
Topic archived. No new replies allowed.