Use a variable. I suggest finding a good introductory book if you're not clear on what they are yet.
#include <iostream>
#include <string>
// ...
std::string filename; // This is a variable of type std::string which holds a series of characters in memory
std::cin >> filename; // Read in the filename from the console
std::ifstream file(filename.c_str()); // c_str() gets a C-style representation of the string (which is what the std::ifstream constructor is expecting)
If the filename can have spaces in it, then cin >>
(which stops input at the first space as well as newline) won't cut it. Instead, you can use getline()
:
getline(cin, filename); // Reads a line of input from the console into the filename variable