Getline isn't working

Hi my name is Mike and I have a computer science project that I am working on. I have tried numerous times on it. It is a sorting program that should sort a text file of 50 students according to last name. The program should include a main function, a Student class, an Address class, and a Date class, for the different data in the file. I am stuck on the Student class. This attempt, I am choosing to import each row from the textfile as a string, and then pass this string to the Student class. For convienience, I brought all the code into one main.cpp file. Here it is:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <istream>
#include <fstream>
#include <string>
#include <sstream>
#include <new>

using namespace std;

//This program will retrieve all the rows from the datafile.txt file, one by one, and pass them to a string variable. The first and last names will be extracted and printed on-screen.

class Student
{
    private:
        //For good practice, all variables should be private...
        string line;
        string firstname, lastname, f, l;

    public:
        //Most functions are then public...
        //These functions will be getters and setters.
        Student(string);          //And also a constructor...
        void setfirstnames(string);
        void setlastnames(string);
        string getfirstnames();     //prototypes
        string getlastnames();

        //void printeverything();
};   //end Student

Student::Student(string minput)
{
    line = minput;
}   //end constructor

void Student::setfirstnames(string firstname)
{
    //stringstream st(line);
    getline(line,firstname, '|');
    cout<<firstname;
}   //end setter 1

void Student::setlastnames(string lastname)
{
    getline(line,lastname,'|');
    cout<<lastname;
}   //end setter 2

string Student::getfirstnames()
{

    return f;
}   //end getter 1

string Student::getlastnames()
{
    return l;
}   //end getter 2

/*void Student::printeverything();
{
    cout<<"First Name:"<<Student::
}   //end printing class
*/
int main()
{
    string master, a, b;  //Declaring the string variable...
    ifstream myfile ("datafile.txt");   //Opens file for input
    //We should use a for loop to get the lines, and call the function to handle them.
    //Student s();
    //But, we need to declare a dynamic array since we are recursively calling a class and its member functions...
    Student * students = new Student[50];
    for(int rows=0; rows<50;rows++)
    {
        getline(myfile, master);
        students[rows]=Student(master);
        //s.Student(string master);
        s.setfirstnames(a);
        s.setlastnames(b);
        //calling an instance of the class...
    }   //end for
    return 0;
}


It, for some reason, returns a "no matching function for call to 'getline(std::string&, std::string&, char)' error. Where did I go wrong?
istream& getline ( istream& is, string& str, char delim );

This is the getline function's prototype (for the getline function that is not part of the istream class).

That means the first parameter should be an istream (not a string), the second should be a string (got that one right), and the third parameter should be a char (you got that one right too).

You tried to do this:
getline(line,lastname,'|');
but I think you meant
getline(myfile,lastname,'|');
Last edited on
So you cannot use getline for string operations?
Not like that, no. Sorry.

Perhaps you wanted to combine these two?
http://cplusplus.com/reference/string/string/substr/
http://cplusplus.com/reference/string/string/find/

(EDIT: You could also theoretically feed the string into an istringstream and continue to use getline if you really wanted.)
http://cplusplus.com/reference/iostream/istringstream/istringstream/

-Albatross
Last edited on
That was my idea from my last attempt! I was trying, in my last attempt, to delete from the string(s) as I went, and to define the process of extracting and deleting data from the string as a function, to be repetitively called later. Would deletion from the main string be necessary?
Last edited on
Topic archived. No new replies allowed.