Problem with getline and file reading.

Apr 24, 2014 at 7:02pm
I am trying to read part of a text file and put first name and last name into a string variable. For some reason or another, this code isn't working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "Nathan_Ward_P-2.h"
#include <fstream>
using namespace std;

int main()
{
    student general[6];
    english eng_student[2];
    history his_student[2];
    math math_student[2];
    
    string filename;
    string lastname, firstname;
    int numstudents;
    cout << "Please enter the name of the file you wish to open:" << endl;
    cin >> filename;
    cout << "File name: " << filename << endl;
    
    ifstream inFile;
    inFile.open("test.txt");
    if (!inFile)
        cout << "Error: file not found." << endl;
    else
    {
        cout << "Processing!" << endl;
    
        inFile >> numstudents;
        for (int counter = 0; counter < numstudents; counter++)
        {
            inFile.getline(lastname, 50, ',');
        }
    
        cout << "Here is the result:" << endl << endl;
    }
    
    return 0;
}


and my text file is:

Bunny, Bugs
math 90 86 80 95 100 99 96 93
Schmuckatelli, Joe
history 88 75 90
Dipwart, Marvin
english 95 76 72 88
Crack Corn, Jimmy
math 44 58 23 76 50 59 77 68
Kirk, James T.
english 40 100 68 88
Lewinsky, Monica
history 60 72 78

the compiler (xcode) give me the error: "No matching member function for call to 'getline'" on line 30. Any help would be appreciated :/
Last edited on Apr 24, 2014 at 7:03pm
Apr 24, 2014 at 7:13pm
There are two different versions of getline. You want the one for std::string. (the other is for character arrays).

http://www.cplusplus.com/reference/string/string/getline/

http://www.cplusplus.com/reference/istream/istream/getline/

Apr 24, 2014 at 7:15pm
Line 30: ifstream.getline expects a char *, not a string.
http://www.cplusplus.com/reference/istream/istream/getline/

Use the string form instead
http://www.cplusplus.com/reference/string/string/getline/
Apr 24, 2014 at 7:19pm
changing line 30 then to:
 
istream& getline(istream& inFile, string& lastname, 50, ',');


Give the error: "expected parameter declarator"

I think there's something I'm missing here. Sorry, still new to programming.
Apr 24, 2014 at 7:34pm
Remove the types from the line:
 
    getline (inFile, lastname, ','); 



Last edited on Apr 24, 2014 at 7:48pm
Apr 24, 2014 at 7:35pm
What you have there is halfway between a function declaration and a function call.

When you call a function you don't need to state the type of each parameter.
getline(inFile, lastname, ',');

You'd also added an extra parameter which isn't required. When using a std::string, you don't need to specify the length.
Apr 24, 2014 at 7:36pm
After doing that I'm still getting an error for no matching function for call to getline.
Apr 24, 2014 at 7:42pm
OK chervil that solved it! taking out the extra parameter for length it worked, as there is no longer an error message. However, when simply using cout to display lastname, it comes up blank.
Apr 24, 2014 at 7:45pm
It depends how your code looks now. in the original code at the top of the page, there was no mention of lastname in the cout statements.
Apr 24, 2014 at 7:47pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "Nathan_Ward_P-2.h"
#include <fstream>
using namespace std;

int main()
{
    student general[6];
    english eng_student[2];
    history his_student[2];
    math math_student[2];
    
    string filename;
    string lastname, firstname;
    int numstudents;
    cout << "Please enter the name of the file you wish to open:" << endl;
    cin >> filename;
    cout << "File name: " << filename << endl;
    
    ifstream inFile;
    inFile.open("/Users/wardnathan95/Desktop/School/Computer Science/Project 2/Project 2/test.txt");
    if (!inFile)
        cout << "Error: file not found." << endl;
    else
    {
        cout << "Processing!" << endl;
    
        inFile >> numstudents;
        for (int counter = 0; counter < numstudents; counter++)
        {
            getline(inFile, lastname, ',');
            getline(inFile, firstname, '\n');
        }
    
        cout << "Here is the result:" << endl << endl;
        cout << firstname << " " << lastname;
    }
    
    return 0;
}


I jsut added the statement on line 35 to test wether or not it worked.
Apr 24, 2014 at 7:55pm
Does your input file start with the number of students? The program expects it, but the sample above starts with "Bunny, Bugs", which isn't a number.

Also you might want to put the cout statement inside the for loop, rather than at the end.
Apr 24, 2014 at 8:03pm
sorry, update to the actual text file:

6
Bunny, Bugs
math 90 86 80 95 100 99 96 93
Schmuckatelli, Joe
history 88 75 90
Dipwart, Marvin
english 95 76 72 88
Crack Corn, Jimmy
math 44 58 23 76 50 59 77 68
Kirk, James T.
english 40 100 68 88
Lewinsky, Monica
history 60 72 78

but now when I try to display lastname, it displays the entire text file like that, except it doesn't show any commas
Last edited on Apr 24, 2014 at 8:06pm
Apr 24, 2014 at 8:08pm
Are you sure? i can certainly foresee problems because of the other data - the file doesn't contain only lastname, firstname. Have you output endl after each name? if not even though read in separately, they might appear all as one long line.
Apr 24, 2014 at 8:11pm
Yes the other data in the text file will be put to use once I can figure this problem out. the exact output is as follows:

Please enter the name of the file you wish to open:
test.txt
File name: test.txt
Processing!
Bugs
Bunny Joe math 90 86 80 95 100 99 96 93
Schmuckatelli Marvin history 88 75 90
Dipwart Jimmy english 95 76 72 88
Crack Corn James T. math 44 58 23 76 50 59 77 68
Kirk Monica english 40 100 68 88
LewinskyHere is the result:

Program ended with exit code: 0


It displays the whole text file, except it took out the commas
Apr 24, 2014 at 8:27pm
Well, if it displays the whole file, that's great! it means your code managed to keep on reading for all of the 6 students.

if you really want to get just this part working first, i suggest you use a modified input file, something like this:
6
Bunny, Bugs
Schmuckatelli, Joe
Dipwart, Marvin
Crack Corn, Jimmy
Kirk, James T.
Lewinsky, Monica

Alternatively add the code to at least ignore the intervening lines until you are ready to add the full details of the code. But really, it feels like I'm doing your job for you. i think you need to pause and think things through a little bit, I think you're heading in a reasonable direction so far.

Apr 24, 2014 at 9:23pm
HOLY CRAP I'M SO RETARDED. The for loop in line 28 has it going 6 times, and for my testing purposes it was constantly reassigning lastname and firstname to different parts of the file. Putting the loop from 0 to 1 in order to test it fixed the problem. OK everything is great and solved now. Thank you so much Chervil!
Topic archived. No new replies allowed.