I am trying to do a little review with classes and pointers. The program that I am working on deals with the grades of the students in a class. What I would like to do is create a record for each student. The algorithm or "idea" I have for this is to use a double pointer to create an array of pointers that in turn point to each student record. Each student record will have member variables for 2 quizzes, a midterm and a final exam. I am not real sure if I am doing this correct. Any and all help is greatly appreciated. Thank you for your time.
#include <iostream>
usingnamespace std;
class StudentRecord
{
//To be filled at a later time
public:
private:
};
int main()
{
char ch;
int numOfStudents;
StudentRecord **students;
cout << "Enter the number of students in the class: ";
cin >> numOfStudents;
cout << endl;
students = new StudentRecord*[numOfStudents];
for(int i = 0; i < numOfStudents; i++)
students[i] = new StudentRecord[1];
cin.get(ch);
return 0;
}
The algorithm or "idea" I have for this is to use a double pointer to create an array of pointers that in turn point to each student record.
Yes, you're doing it correctly, if that is what you want, but my question is why the array of pointers? It seems totally unnecessary. Just allocate an array of students and be done with it.
Better yet, use a std::vector.
BTW, don't forget to deallocate both your array of pointers and the individual student records, otherwise you have a memory leak.