1

I need to get only specific characters from a text file. I am using the getline() function in C++. my compiler keeps giving me the error that there is no matching member function call for getline(), how can I fix that? I'm trying to pull last names and scores from the file.

the file looks like:

Weems 50 60

Dale 51 60

Richards 57 60
...

Here's the code I'm trying:

#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

int main ()
{
    //input variables
    float GradeScore;
    float TotalPoints;
    float GradePercent;
    string LastName;

    ifstream myFile;

    //open file
    myFile.open ("/Users/ravenlawrence/Documents/TestGrades.rtf",ios::in);
    // if file is open
    if (myFile.is_open()) {
        while(!myFile.eof()) {
            string data;
            getline(myFile,data); //reading data on line
            myFile.getline(LastName, ' ');//storing data in LastName 
            myFile.getLine(GradeScore,' ');//storing data in GradeScore 
            myFile.getLine(TotalPoints,' ');//storing data in Total Points 
            cout << LastName << endl;
            // cout<<data<<endl; //print it out
        }
    }
    return 0;
}
Eziz Durdyyev
  • 1,110
  • 2
  • 16
  • 34
ravenchannell
  • 11
  • 1
  • 4
  • 1
    `LastName` is a string, so need to use the free function, just like `data`. – Bo Persson Mar 01 '18 at 02:51
  • I'm new to C++. What is the free function? – ravenchannell Mar 01 '18 at 02:59
  • 2
    Possible duplicate of [How do I fix a "No instance of overloaded function" error with cin.getline?](https://stackoverflow.com/questions/10625701/how-do-i-fix-a-no-instance-of-overloaded-function-error-with-cin-getline) – xskxzr Mar 01 '18 at 03:04
  • @ravenchannell - A free function is a function that is not a member of a class. You *are* using one in `getline(myFile,data);`. – Bo Persson Mar 01 '18 at 03:59
  • `getline()` extracts raw input. It does not convert the input to things like floats or ints. You can only call it with a string. If you want to input a number, use the `>>` operator. Also, the member function `getline()` only accepts `char *` (C-style string) as the place to store the results. Use the free function as described in the other comments for C++ strings. – eesiraed Mar 01 '18 at 05:37
  • Also, you're one of the many people I've seen here with the [`eof()` in loop condition problem](https://stackoverflow.com/q/5605125/9254539). – eesiraed Mar 01 '18 at 05:39
  • myFile.getline() is a member function; in contrast, getline() is not a member of a class, but rather a free function. You shouldn't read anything without checking the result of that read. See also https://stackoverflow.com/questions/35974402/reading-getline-from-cin-into-a-stringstream-c – Kenny Ostrom Mar 01 '18 at 14:20

2 Answers2

0

Begin with a design, breaking down the work into small steps:

open file
loop, reading line from file while more lines
    split line into fields
    convert fields into variables
    display variables

Now tackle each step

// open file
ifstream myFile ("/Users/ravenlawrence/Documents/TestGrades.rtf",ios::in);
if( ! myFile ) {
  cerr << "cannot open file\n";
  exit(1);
}

//loop, reading line from file while more lines
string data;
while( getline( myFile, data ) ) {

   // split line into fields
   std::stringstream sst(data);
   std::string a;
   std::vector<string> vfield;
   while( getline( sst, a, ' ' ) )
       vfield.push_back(a);

   // ignore lines that do not contain exactly three fields
   if( vfield.size() != 3 )
      continue;

   //convert fields into variables
   LastName = vfield[0];
   GradeScore = atof( vfield[1].c_str() );
   TotalPoints = atof( vfield[2].c_str() );

   // display
   ...
}
ravenspoint
  • 19,093
  • 6
  • 57
  • 103
0

You do not need to use the function getline here, you can read file word by word.Secondly, you need to close the file after it reaches eof. Here is the code:

   int main()
   {
       //input variables
         float GradeScore;
         float TotalPoints;
         float GradePercent;
         string LastName;

         ifstream myFile;

       //open file
         myFile.open("check.txt", ios::in);
      // if file is open
         if (myFile.is_open()) {

           while (!myFile.eof()) {

              myFile >> LastName;//storing data in LastName 
              myFile >> GradeScore;//storing data in GradeScore 
              myFile >> TotalPoints;//storing data in Total Points 

              cout << LastName << endl;
           // cout<<data<<endl; //print it out
           }

         myFile.close();
      }
      system("pause");
      return 0;
      }

Rather than checking if file is open or not a better approach is to check if file exist or not:

        if(!myfile)
        {
          cout<<"error!file donot exist";
         }
  • " read file word by word" The snag with this approach is that if there is any error in the file, say a missing field, the program will become confused and read rubbish from all the rest of the file. Better to start fresh with each line, so a single error does not turn everything into garbage. – ravenspoint Mar 02 '18 at 14:23