Using a variable for a filename
i seem to be having trouble using a variable as a file name for an ifstream.
i am making a program that is quite simple. all it does is ask for a filename, reads the file and displays it to the screen. my source code is
Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
string file;
char letter;
ifstream in_stream;
cout << "Please enter the name of \n"
"the file you wish to open. \n"
"Please use only text files. \n";
cin >> file;
in_stream.open(file);
if (in_stream.fail())
{
cout << "File Failed To Open";
system("pause");
exit(1);
}
do
{
in_stream.get(letter);
cout.put(letter);
}
while(! in_stream.eof());
cout << endl;
system("pause");
}
the underlined line is where i receive errors. I'm sure that I should be able to do this. Im following the syntax out of a book. Can someone tell me whats wrong?