Problem retrieving values from an array

I've written a little Student class as part of a project I'm working on. However, I'm stuck on trying to retrieve values from an array. I understand my program is pretty messy at the moment but it's in the very initial stages of design so I've kind of got things all over. Please excuse that!

Try as I might, I can't seem to get past this. Help would be appreciated =)

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

using namespace std;

class Student
{
public:
	Student()
	{
	}
	Student(string n, string i)
	{
		name = n;
		ID = i;
	}
	void setName(string s)
	{
		name = s;
	}
	string getName()
	{
		return name;
	}
	void setID(string s)
	{
		ID = s;
	}
	string getID()
	{
		return ID;
	}
	void calcGPA()
	{
	}
	string getGPA()
	{
		return GPA;
	}
	void setAns(string s, int i)
	{
		this->answers[i] = s;
		//cout << answers[i];
	}
	string getAns(int i)
	{
		return this->answers[i];
	}
private:
	string name;
	string GPA;
	string ID;
	string answers[20];
};
void inputTest(vector<Student>);

int main()
{
	string key[] = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"};
	vector<Student> stu;
	int newStudents =0;
	cout << "How many students grades will you be entering? ";
	cin >> newStudents;

	for(int i =0; i< newStudents; i++)
	{
		string name, ID;
		cout << "Enter the name for student " << (i+1) << ": ";
		cin >> name;
		cout << "Enter the ID number for student " << (i+1) << ": ";
		cin >> ID;
		stu.push_back(Student(name, ID));
	}

	inputTest(stu);

/*This is where I need help
* This should print out all the answers stored in the answers 
* array in the student class, however, it just prints
* Answers were: 20 times*/
	for(int i =0; i < 20; i++)
		cout << "Answers were: " << stu[0].getAns(i) << endl;
	
	return 0;
}

void inputTest(vector<Student> vec)
{
	const int NUM_QUESTIONS = 20;
	string ans;
	for (int j =0; j < vec.size(); j++)
	{
		cout << "Student: " << vec[j].getName() << endl;
		for (int i = 0; i < NUM_QUESTIONS; i++)
		{	
			cout << "Enter student's answer for question " << (i+1) << ": ";
			cin >> ans;
			vec[j].setAns(ans, i);
			cout << "Setting answer for " << vec[j].getName() << endl;
		}
	}
}
Line 84 is correct. The problem is that inputTest() takes an std::vector<Student>, not a reference or pointer to a std::vector<Student>. This means that the call to it creates a copy of the vector and its element and passes that, instead. Changes made to that copy are not reflected on the original.
So that you don't need to do much rewriting, change the type of the parameter to 'std::vector<Student> &'.
Last edited on
Arg ><, I'm a java refugee so I keep forgetting about passing by reference instead of by value. Thank you for pointing that out!
Topic archived. No new replies allowed.