-1

I have looked at a couple of links such as this and this.

Unfortunately I'm just to new of a programmer to figure it out. I would like to have the following as the line while( getline(getline(fin, line) ) because I'm trying to read in the entire line of text from a file. Then I'm trying to figure out if there are any similar words or numbers in that file. I'm writing this in Microsoft visual studio 2012.

#include <iostream>
#include <fstream>
#include <sstream>
#include <cctype>
#include <string>
using namespace std;

// main application entry point
int main(int argc, char * argv[])
{
    string filename;
    ifstream inFile;

    // request the file name from the user
    cout << "Please enter a filename: ";

    // stores the users response in the string called filename
    cin >> (std::cin, filename);

    // opens the file
    inFile.open(filename.c_str());

    // if the file doesn't open
    if (!inFile)
    {
        cout << "Unable to open file: " << filename << endl;

        return -1;

    } // end of if( !inFile )

    // while( getline(getline(fin, line) ) gives me the same error
    while (getline())
    {}

    // close the file
    inFile.close();

} // end of int main( int argc, char* argv[])
user26093
  • 19
  • 6
  • 1
    Just curious: What do you think `cin >> (std::cin, filename)` does? – David G Jan 26 '14 at 01:06
  • I thought it did: // stores the users response in the string called filename but after your comment it would appear that I'm completely wrong. – user26093 Jan 26 '14 at 01:12
  • No, you're right that it does that, it's just that you're doing it in an unnecessary way. `(std::cin, filename)` is the same as `filename` because the comma operator `,` returns the rightmost operand. All you really need is `std::cin >> filename`. – David G Jan 26 '14 at 01:14
  • What's the full error message? – kirbyfan64sos Jan 26 '14 at 01:15

1 Answers1

0

why am I getting a error: no instance of an overloaded function “getline” matches the argument list here?

Because you called std::getline() without any arguments, while std::getline() does require arguments:

while( getline() )
{
}

What std::getline() takes, however, is

  1. an stream& (where the input comes from)
  2. an std::string& (where the input ends up)
  3. optionally a char (a delimiter, the default is '\n')

Something like this should do:

std::string line;
while( std::getline(inFile, line) ) {
  // process line 
}

Note that your code is quite a mess. Let's walk through it:

int main(int argc, char * argv[])

Since you are not using argc and argv, why pass them? Your compiler should warn you that they aren't used – which is just noise that might distract you from a compiler diagnostic that points at a real issue. Do this instead:

int main()

and the warning is gone.

string filename;
ifstream inFile;

Why define these at the top of the function, when they are used only further down? In C++, it is considered good style to define objects as late as possible, preferably when they can be initialized.

using namespace std;

This is a bad idea that might hurt you badly. Just don't do it.

cin >> ( std::cin, filename );

I have no idea what this is supposed to do, let alone what it actually does, assuming it compiles. What you want instead, is this: std::cin >> filename. Note, however, that this prevents file names containing whitespace. Should that be a problem, employ std::getline() instead.

inFile.open( filename.c_str() );

This is where inFile should have been defined:

std::ifstream inFile( filename.c_str() );

Finally, your explicit closing of the file

inFile.close();

is unnecessary. The destructor of std::ifstream takes care of that anyway.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • I still get the error when I do this: while( getline(fin, line)) {} – user26093 Jan 26 '14 at 01:06
  • 1
    @user26093 I think the code you're showing us **isn't** the code that you actually have....Show us the code you actually have please. – David G Jan 26 '14 at 01:11
  • @user26093 `getline(fin, line))`? What's `fin`? – jfly Jan 26 '14 at 01:12
  • yes. It gave me the same error. fin is supposed to be fileIn. – user26093 Jan 26 '14 at 01:12
  • I'm just trying to read in every line of a file, do work on it, then read in the next line.. – user26093 Jan 26 '14 at 01:15
  • I see you have put `while( getline(getline(fin, line) )` into your question, complaining that it doesn't work either. I wonder what you think this nesting of `getline()` does? And the parentheses do not even match, so there's no way it could compile... – sbi Jan 26 '14 at 01:29
  • @user26093: Have a look at [this answer](http://stackoverflow.com/a/6892829/140719), [this one](http://stackoverflow.com/a/11881316/140719), and [this one](http://stackoverflow.com/a/1418587/140719). If that doesn't help, simply browse through [all my answers containing `std::getline()`](http://stackoverflow.com/search?q=user%3A140719+getline). I have given dozens of answers for it. – sbi Jan 26 '14 at 01:30