Giving "callable" names to object array

Hey guys, so I'm doing C++ at my school but it's independent study and thus I've been making up my own projects. On this particular one I've hit a roadblock. Currently it allows grade data to be inputed for an array of student objects and then have it later viewed (for the duration of runtime). Each array number contain's its own grade data because it's suppose to represent a student and so the user has use numbers which isn't ideal. What I want to do is make it so the user can input a student's name and then that would store the grade data. I'm not really sure as to how to go about this and would appreciate any advice.
Also this isn't vital but it'd be great if the grade data could save itself for later viewing even after the program has closed.

-Cheers and Thanks

(compiled in Visual Studio 2008)
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
105
106
107
108
109
110
111
112
113
114
115
//Program that allows a 101 students (starting at zero) to have their grades and semester hours saved
//and stored so they can be viewed/edited again for the duration of the program

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Student
{

public:

	float newStudent(int studentNum)
	{	
		//Collects Data if student not in system
			cout << "-Record not found please enter student's information-" << endl;
			cout << "Enter previous semester hours: \n";
			cin  >> semesterHours;
			cout << "Enter previous gpa: \n";
		    cin >> gpa;

		//displays grade from before
			cout << "before: " << studentNum <<  " = (" << semesterHours
				<< ", "			<< gpa
				<< ")"				<< endl;
		
		//collects data for this semester
			cout << "Enter this semester's hours: \n";
			cin >> semesterHoursNow;
			cout << "Enter this semester's gpa: \n";
			cin >> gpaNow;
	
			cout << "Adding " << semesterHoursNow << " hours with a grade of " << gpaNow << endl;
			
		//Grade Calculation
			float weightedGPA;
			weightedGPA = semesterHours * gpa;

			semesterHours += semesterHoursNow;
			weightedGPA += gpaNow * semesterHoursNow;
			gpa = weightedGPA / semesterHours;

		//Displays new grade
			cout << "After: "  << studentNum <<  " = ("  << semesterHours
				<< ", "			<< gpa
				<< ")"				<< endl << endl;
			return 0;
}	

	int oldStudent(int studentNum)
		{
		//displays grade from before
			cout << "before: " << studentNum <<  " = (" << semesterHours
				<< ", "			<< gpa
				<< ")"				<< endl;
	
		//collects data for this semester
			cout << "Enter this semester's hours: \n";
			cin >> semesterHoursNow;
			cout << "Enter this semester's gpa: \n";
			cin >> gpaNow;
			
			cout << "Adding " << semesterHoursNow << " hours with a grade of " << gpaNow << endl;
			
		//Grade Calculation
			float weightedGPA;
			weightedGPA = semesterHours * gpa;

			semesterHours += semesterHoursNow;
			weightedGPA += gpaNow * semesterHoursNow;
			gpa = weightedGPA / semesterHours;

		//Displays new grade
			cout << "After: " << studentNum <<  " = ("  << semesterHours
				<< ", "			<< gpa
				<< ")"				<< endl << endl;
			return 0;
		}

	//Declares variables used in functions
	int semesterHours;
	float gpa;
	int semesterHoursNow;
	float gpaNow;
};
int main(int nNumberofArgs, char* pszArgs[])
	{
//Initializes object array and loop variable
		int repeat;
		int studentNum;
		Student s[102]= {0}; 

	//test student
		s[3].semesterHours = 55;
		s[3].gpa = 3;

	//determines if new or old student funct should be called
		do{
			cout << "Enter Student's number (0-100): ";
			cin >> studentNum;
				if  (s[studentNum].semesterHours > 0)
				{
					s[studentNum].oldStudent(studentNum);	
				}
				else 
				{
					s[studentNum].newStudent(studentNum);
				}
	//repeat program?
		cout << "Would you like to view/edit another student's record?(1)\nOr exit the program?(2)\n";
		cin >> repeat;
	} while (repeat != 2);
	return 0;
}
That's done with a map:

1
2
3
4
5
6
7
#include <map>
[...]
map<string,Student> students;
string name;
getline(cin,name);
[...]
students[name].newStudent(studentNum);


http://www.cplusplus.com/reference/stl/map/

Saving data is done with files.
Hmmm, I put the map and string declarations below my comment of "//Declares variables used in functions" and it says "name" is an undeclared identifier. "String" didn't turn blue nor did "map" so I'm assuming something is wrong with the compiler?
Huh? They don't belong into the class. It could get a name member, but that's obviously separate from the input variable.
The only things that belong into a class are attributes that describe an instance of that class (a student) and operations (methods) you can perform on a student (and sometimes static members).

"String" didn't turn blue nor did "map" so I'm assuming something is wrong with the compiler?

Editor, not compiler. And no, string and map aren't builtin types, so editors don't highlight them in the same color as int, double etc.. std::string and std::map are classes that are part of the C++ standard library.
Last edited on
Oh, ya that doesn't make sense. So I put them in main, (is that right?), and it compiled sort of but stopped at getline so I did this "std::getline(std::cin, name);" and that sort of worked but now it's saying
"Unhandled exception at 0x004015aa in arrays.exe: 0xC0000005: Access violation reading location 0xc982d268."
and I have to end the program with task manager :/

And thanks a lot for the explanation that clears some things up.
Last edited on
There isn't much room for access violations when using maps, so... post your new code.
Also, I wouldn't say something works when it causes an access violation. It's very much broken.
Last edited on
haha yes it's screwed up but the point is it's slowly getting better I think anyway here's the lovely new code:
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <cstdio>
#include <cstdlib>
#include <map>
#include <iostream>
#include <string>
using namespace std;

class Student
{

public:

	float newStudent(int studentNum)
	{	
		//Collects Data if student not in system
			cout << "-Record not found please enter student's information-" << endl;
			cout << "Enter previous semester hours: \n";
			cin  >> semesterHours;
			cout << "Enter previous gpa: \n";
		    cin >> gpa;

		//displays grade from before
			cout << "before: " << studentNum <<  " = (" << semesterHours
				<< ", "			<< gpa
				<< ")"				<< endl;
		
		//collects data for this semester
			cout << "Enter this semester's hours: \n";
			cin >> semesterHoursNow;
			cout << "Enter this semester's gpa: \n";
			cin >> gpaNow;
	
			cout << "Adding " << semesterHoursNow << " hours with a grade of " << gpaNow << endl;
			
		//Grade Calculation
			float weightedGPA;
			weightedGPA = semesterHours * gpa;

			semesterHours += semesterHoursNow;
			weightedGPA += gpaNow * semesterHoursNow;
			gpa = weightedGPA / semesterHours;

		//Displays new grade
			cout << "After: "  << studentNum <<  " = ("  << semesterHours
				<< ", "			<< gpa
				<< ")"				<< endl << endl;
			return 0;
}	

	int oldStudent(int studentNum)
		{
		//displays grade from before
			cout << "before: " << studentNum <<  " = (" << semesterHours
				<< ", "			<< gpa
				<< ")"				<< endl;
	
		//collects data for this semester
			cout << "Enter this semester's hours: \n";
			cin >> semesterHoursNow;
			cout << "Enter this semester's gpa: \n";
			cin >> gpaNow;
			
			cout << "Adding " << semesterHoursNow << " hours with a grade of " << gpaNow << endl;
			
		//Grade Calculation
			float weightedGPA;
			weightedGPA = semesterHours * gpa;

			semesterHours += semesterHoursNow;
			weightedGPA += gpaNow * semesterHoursNow;
			gpa = weightedGPA / semesterHours;

		//Displays new grade
			cout << "After: " << studentNum <<  " = ("  << semesterHours
				<< ", "			<< gpa
				<< ")"				<< endl << endl;
			return 0;
		}

	//Declares variables used in functions
	int semesterHours;
	float gpa;
	int semesterHoursNow;
	float gpaNow;
};
int main(int nNumberofArgs, char* pszArgs[])
	{
//Initializes object array and loop variable
		int repeat;
		int studentNum;
		Student s[102]= {0}; 
		std::map<string,Student> students;
		std::string name;

	//test student
		s[3].semesterHours = 55;
		s[3].gpa = 3;

	//determines if new or old student funct should be called
		do{
			cout << "Enter Student's number (0-100): ";
			std::getline(cin, name);
			//cin >> studentNum;
				if  (s[studentNum].semesterHours > 0)
				{
					//s[studentNum].
					students[name].oldStudent(studentNum);	
				}
				else 
				{
					//s[studentNum].
					students[name].newStudent(studentNum);
				}
	//repeat program?
		cout << "Would you like to view/edit another student's record?(1)\nOr exit the program?(2)\n";
		cin >> repeat;
	} while (repeat != 2);
	return 0;
}
Last edited on
Well, in line 104 you access s[studentNum] and studentNum is uninitialized. You don't need the s array anymore, so you should get rid of it.
Awesome! That made it work thank you so much Athar, one thing I had to do though was "cin >> name;" instead of the getline bit because it was passed over when I repeated the loop. Is there a reason for this?
Last edited on
If you are using ` cin >> someVar ' in your code before a call to getline() the carriage return is probably still in the buffer so getline extracts it and goes on about its merry way. Try cin.sync() before calling getline().
That's perfect, thanks everyone :D
Topic archived. No new replies allowed.