I've re-formatted the code, the indentation was making it hard to follow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
ifstream infile(line.c_str());
cout<< "Enter a string line: ";
if (getline(cin, line))
;
{
cout << "you entered: " << line << endl;
return true;
}
if (!infile)
{
cout << "Error reading data.txt" << endl;
return -1;
}
}
|
One of the original problems remains. At line 10, an attempt is made to open the input file named "" (that is, an empty string for the name).
At line 14, there is an if statement which ends with a semicolon ( I put it on the next line for clarity). Remove the semicolon.
The code in braces on lines 17 to 20 will always be executed, thus the program terminates with the return statement at line 19 (though function main should return an int).
The return statement at line 19 is not needed.
What I suggest is that line 10,
ifstream infile(line.c_str());
is moved after the user has entered the filename.
Also, it would be useful if the prompt explained to the user what they needed to do, so instead of
cout<< "Enter a string line: ";
I would put something like
cout<< "Enter name of input file: ";