Dynamically Allocated Structure Array from Data File- Simple?

I am attempting to write a function that returns a pointer to a structure array. According to the assignment details, this array MUST be dynamically allocated.

The data file we read from contains
2
John Doe
T00001264
0
1
0.00
Jane Doe
T00012345
3
0
3.62


Where the first number is the number of student structs to be in the array

Here is my student struct:
1
2
3
4
5
6
7
struct Student {
	string name;
	string studentID;
	Year year;
	Semester semester;
        float gpa;
                };


and my function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Student *readStudents(std::ifstream &inp, int &num_students) 
{
	inp>>num_students;
	
	Student *studentptr;
	studentptr= new Student[num_students];
	
	for (int i=0; i<num_students; i++)
	{
		inp.getline(studentptr[i].name);
		inp>>studentptr[i].studentID;
		inp>>studentptr[i].year;
		inp>>studentptr[i].semester;
                inp>>studentptr[i].gpa;
	}
	return studentptr;
}


When I try to compile I get an error of


C:\Users\Owner\Documents\Programming>g++ -o students students.cpp
students.cpp: In function `Student* readStudents(std::ifstream&, int&)':
students.cpp:130: error: no matching function for call to `std::basic_ifstream<c
har, std::char_traits<char> >::getline(std::string&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/istream.tcc:582: note: candid
ates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Trait
s>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std
::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/istream:399: note:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getli
ne(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<cha
r>]
students.cpp:132: error: no match for 'operator>>' in 'inp >> ((+(((unsigned int
)i) * 20u)) + studentptr)->Student::year'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/istream.tcc:87: note: candida
tes are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits
>::operator>>(std::basic_istream<_CharT, _Traits>&(*)(std::basic_istream<_CharT,
_Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]

Which continues for lines and lines of code, essentially repeating the message for all four lines of file input.


I'm sure this is a completely idiotic mistake of me either overlooking something incredibly simple or coding it entirely wrong in the first place, but I would appreciate any help at all that can be offered in my error.

Thanks!
getline has two versions. The one you used accepts a char* (C-string):
http://www.cplusplus.com/reference/iostream/istream/getline/

The one you want is used a bit differently:
http://www.cplusplus.com/reference/string/getline/
Thank you, I believe that solved the "getline" error. However I'm still receiving the miles of error off the ">>" operator.

Once again I'm sure it's probably a simple fix
I completely forgot to mention that both year and semester are custom enumerated types. Will the ">>" operator not read an enum correctly from a file? And if so, which operator should I be using?
Topic archived. No new replies allowed.