I am trying to use the getline() function and nothing is working I've tried doing fin.getline(name), and I get an error of "no instance of overloaded function matches the argument list" then I tried getline(fin, name) which compiles fine but it assigns "" an empty string as the name. The problem is in course.cpp in the ReadFromFile function
coursedata.txt
6
cs 1400
Jake Johnson 2573 3.23
Leslie Porter 3845 3.11
Martie Salt 9300 2.46
Jordan Ngatai 0433 3.74
Cameron Slater 3004 1.84
Ashley Dickerson 4848 2.23
int main()
{
Course *course;
int size;
ifstream fin;
string filename;
cout << "Enter filename: ";
cin >> filename;
fin.open(filename);
if (fin.fail())
{
cout << endl << "cannot open file!" << endl;
}
fin >> size;
course = new Course(size); // create a new Course of this many students
course->ReadFromFile(fin, size); // read all information for this course
course->SortByName();
course->Output(); // output all information for this course
}
course.cpp
#include <iostream>
#include<fstream>
#include "Course.h"
#include <string>
using namespace std;
Course::Course(int size)
{
student=new Student[size];
}
void Course::Set(string named, int sized)
{
name = named;
size = sized;
}
void Course :: SortByName()
{
bool done = false;
while (!done)
{
done = true;
for (int n = 0; n<size - 1; n++)
if ((student+n)->GetName() > (student+n+1)->GetName())
{
Swap( *(student+n), *(student+n+1));
done = false;
}
}
While I am not 100% sure, but, my guess is you need to flush out the buffer with a cin.ignore () and cin.clear() after you use cin (in main.cpp) then the cin.getline should work. Also, please remember to use the code tags it is nicer on the eyes.