Getting file into dynamic array with class

I am trying to read from a which first input is how many records are in. The rest is the id of the student, first name, last name, and then exam grade. However, I can not get the input from the file into the array. Any help from anyone?
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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

class Student
{private:

public:
	Student();
	Student(int id, string firstName,string lastName,int grade);
	int id;
	string firstName;
	string lastName;
	int grade;
};

int main()
{
	int size;
	ifstream fin;
	ofstream fout;
	cout <<" Will be creating a program that can manipluate files\n";
	fin.open("input.txt");
	fin >> size;
	Student *records;
	records=new Student[size];
	while(!fin.eof())
	{
		for(int i=0;i<size;i++)
			fin <<records[i];
	}
	system("pause");
	return 0;
}
you set up a constructor in your Student class but you never defined it.

You would need to do something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Student
{
 private:
 public:
    int id, grade;
	string firstName;
	string lastName;
        Student(int, string, string, int);
};

Student::Student(int a, string b, string c, int d){
    id = a;
    firstName = b;
    lastName = c;
    grade = d;
}



but if you aren't going to have any private variables you may as well use a struct instead of a class. A struct is pretty much the same thing as a class just that all the variables are public by default instead of private like a class.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Student
{
    int id, grade;
	string firstName;
	string lastName;
        Student(int, string, string, int);
};

Student::Student(int a, string b, string c, int d){
    id = a;
    firstName = b;
    lastName = c;
    grade = d;
}
Last edited on
Thank you! I have now defined the constructor.This is for an assignment and it needs to be in a class. I just have no idea on how to go about setting all of this into an array.
I also keep getting a error that states" unresolved external symbol"
Topic archived. No new replies allowed.