#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string filename;
string input_line; // Input line for text entry
cout << "Enter a file name and press ENTER: ";
cin.getline(cin, filename); //THIS IS THE ERROR LINE
ofstream file_out(filename);
if (! file_out) {
cout << "File " << filename << " could not be opened.";
return -1;
}
cout << "File " << filename << " was opened." << endl;
while (1) {
cout << "Enter line (@@@ to quit)>>";
cin.getline(cin, input_line);
if (strcmp(input_line, "@@@") == 0)
break;
file_out << input_line << endl;
}
file_out.close();
return 0;
}
The error occurs on line 15( the one with the comment "THIS IS THE ERROR LINE" ). I'm using Code::Blocks on windows, and here is the error message.
cin.getline(cin, filename); //THIS IS THE ERROR LINE
should be
getline(cin, filename); //c++ string getline
or
cin.getline(filename, SOME_LENGTH_PARAM); //but filename here would have to be a c-string, not the c++ string it is
Oh, and this:
error: no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::istream&, std::string&)'|
means
no matching function call to `[input stream].getline([input stream type, c++-string type])
Because
basic_istream means input stream
std::istream& means input stream
std::string& means c++ string
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.