newJoe
newJoe

Reputation: 1

**Compiler error** - getline() function not accepting first parameter "std:ifstream" what is my issue?

I'm writing a program for my homework that counts words and lines. I'm wondering why i get the error: "no instance of overloaded function "getline" matches the argument list. Argument types are (std::ifstream, int)" I was certain "infile" was of std::ifstream argument type. Could the problem be visual studio or am i quick to blame something without prior knowledge? P.S i searched a bit but would not find a thread with exactly the same problem.. there are similar ones but they end up being that someone put a string of the file name and not the stream itself. Also keep in mind i'm in the middle of writing this i didn't finish yet.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;



int main()
{

ifstream infile;
infile.open("Lear.txt");
string word;
int countWords = 0, countLines = 0;
while (!infile.eof())
{
    infile >> word;
    countWords++;
    getline(infile, countLines); //issue area here at infile
    countLines++;

}
cout << "Words: " << setw(9) << countWords << endl;
cout << "Lines: " << setw(9) << countLines << endl;
infile.close();

}

Upvotes: 0

Views: 291

Answers (1)

Blastfurnace
Blastfurnace

Reputation: 18652

There is no std::getline overload that takes an int second parameter. I assume you meant to pass your std::string variable instead.

getline(infile, word);

You should remove the infile >> word; line or decide whether you want to use it or std::getline. I don't think you want both in this case.

This will fix the compiler error but not your program logic. If you use std::getline you'll have to parse each line to count words.

Upvotes: 1

Related Questions