Page 1 of 1

Having code troubles with cin.getline

Posted: Sun Jul 26, 2009 4:13 pm
by JaxDragon
Im writing a console program, and I am having some serious problems.

First off, here is the code

Code: Select all

#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.

Code: Select all

error: no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::istream&, std::string&)'|
Usually I don't have problems debugging my code, but that error message really confuses me.

Re: Having code troubles with cin.getline

Posted: Sun Jul 26, 2009 7:48 pm
by MarauderIIC
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

Re: Having code troubles with cin.getline

Posted: Sun Jul 26, 2009 9:26 pm
by JaxDragon
Thanks.

I was originally using cin.getline(chararraynamehere, legnthofchararray-1), but I wanted to use strings instead.