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:
#include <iostream>
#include <istream>
#include <fstream>
#include <string>
#include <sstream>
#include <new>
usingnamespace 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,'|');
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?