Having users enter filenames in c++?

In my program I want the users to create their own files, so that they can come back and use the same data later instead of just entering the data everytime. I'm not sure how to do it though, I was trying:
 
ifstream(""<<name<<".dat");


but it didn't seem to like that, anyone have any other ideas? Thanks.
Maybe just ifstream(name".dat"); would work. You could try that
I think you should construct the file name first, then use it as argument to the ifstream constructor. Something like

1
2
3
4
5
6
7
8
9
ifstream fs;
string fn;

cout << "Enter filename: ";
cin >> fn;
fn += ".dat";

fs.open(fn.c_str());  // To get const char* from string

This doesn't modify the name string:
1
2
3
string name;
//set name to whatever you want
ifstream file( (name+".dat").c_str() );
awesome! thanks it works perfectly!
Topic archived. No new replies allowed.