Having code troubles with cin.getline

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Having code troubles with cin.getline

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Having code troubles with cin.getline

Post 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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Re: Having code troubles with cin.getline

Post by JaxDragon »

Thanks.

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