Initialization list

I am having trouble initialize an array of objects. This is what I have;

this are the constructors in the Student Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Student()
	{
		info.firstName = "Mike";
		info.lastName = "Jones";
		info.id = "A0001121";
		info.creditHours = 24;
		info.GPA = 3.2;

	}

Student(string first, string last, string ids, int credit, double gpa)
	{
		info.firstName = first;
		info.lastName = last;
		info.id = ids;
		info.creditHours = credit;
		info.GPA = gpa;
        }



	


This is the array
1
2
3
Student student[NUM_STUDENTS] = {Student (Mark, Smith, A00356, 56, 3.8),
				       Student (John, Armstrong, A00054, 54, 4.0),
			              Student (Steven, Stewart, A00784, 86, 2.1)};


As of right now all the names of students, id and gpa give out an error message sayig that they are undefined
Last edited on
Those names need to be strings:

1
2
3
Student student[NUM_STUDENTS] = {Student ("Mark", "Smith", "A00356", 56, 3.8),
				       Student ("John", "Armstrong", "A00054", 54, 4.0),
			              Student ("Steven", "Stewart", "A00784", 86, 2.1)};

Wazzak
Last edited on
Thanks, I didnt see that lol

Now I need to display the arrays but its now working, I have this:

1
2
3
4
5
6
7
8
9
void displayall(Student students[],int in)
{
	
	for(int i = 0;i<in;i++)
	{
		
	cout<<i+1<<" "<<students[i].getFirst()<<" "<< students[i].getLast()<<" "<< students[i].getid()<<"       "<<students[i].getCreditHours()<<"          "<< students[i].getGPA()<<endl;
	}
}
I need to see the implementation of "Student" before I can diagnose the problem.

Wazzak
Last edited on
You mean the Student class right?

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Student
{
private:
	Students info;

public:
	Student()
	{
		info.firstName = "Mike";
		info.lastName = "Jones";
		info.id = "A0001121";
		info.creditHours = 24;
		info.GPA = 3.2;

	}
	Student(string first, string last, string ids, int credit, double gpa)
	{
		info.firstName = first;
		info.lastName = last;
		info.id = ids;
		info.creditHours = credit;
		info.GPA = gpa;
	}
	void setFirst(string first)
	{
		info.firstName = first;
	}
	string getFirst()
	{
		return info.firstName;
	}
	void setLast(string last)
	{
		info.lastName = last;
	}
	string getLast()
	{
		return info.lastName;
	}
	void setid(string sid)
	{
		info.id = sid;
	}
	string getid()
	{
		return info.id;
	}
	void setCreditHours(int hours)
	{
		info.creditHours = hours;
	}
	int getCreditHours()
	{
		return info.creditHours;
	}
	void setGPA(double gpa)
	{
		info.GPA = gpa;
	}
	double getGPA()
	{
		return info.GPA;
	}
};
A little more information,when trying to display; if I take out the in argument for the displayall function it does display the array of objects, but since the array is of 10 objects it displays the first three that I initialized and from 4 to 10 it displays what I put in the Student constructor repeatedly, I only want it to display the three objects that i initialized.
What are you passing to displayall in the second argument?
You should be passing a 3. Sounds line you're passing 10.
//I'm a beginner but:

I think you should do something like that:

int size=3;
displayall(student[NUM_STUDENTS],size);

Topic archived. No new replies allowed.