error with getline() function.

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

main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "Course.h"
using namespace std;

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;
}
}

}
void Course::Output()
{
for (int n = 0; n < size; n++)
{
cout << (student + n)->GetName() << " "<<(student + n)->GetNumber();
cout << " " << (student + n)->GetGrade() << endl;
}
}
void Course::ReadFromFile(ifstream &fin, int sizee)
{
size = sizee;
//fin.getline();
getline(fin, name);
//fin.getline(name);
Set(name, size);
student->ReadFromFile(fin, size);
}
void Course::Swap(Student &x, Student &y)
{
{
Student temp;
temp = x;
x = y;
y = temp;
}
}

course.h

#include <iostream>
#include <string>
#include <fstream>
#pragma once
#include "Student.h"

using namespace std;

class Course
{
private:

string name;
int students, size;
Student *student;

public:
Course(int size);
void ReadFromFile(ifstream &fin, int sizee);
void SortByName();
void Output();
void Set(string named, int sized);
void Swap(Student &x, Student &y);
};
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.
Last edited on
Topic archived. No new replies allowed.