Hey there, I'm having trouble with this second constructor, and any help or guidance would be appreciated. I'm simply clueless as to what I need to put inside it.
Here are the instructions and the constructors: The default Constructor should build a dynamic array of size 3 for the grades. Another constructor must receive an integer specifying how many grades the student will store in the array (the size of the array).
Could one of you please explain to me why is this second constructor necessary? And would it be something like this?
# include <iostream>
# include <iomanip>
# include <string>
# include <fstream>
usingnamespace std;
#ifndef STUDENT_H
#define STUDENT_H
// Class
class Student{
private:
string name,
last;
int age;
char gender;
int *gptr;
public:
Student();
Student(int size);
int CreateStudent();
int StoreStudent(string, string, string, int, char, int[]);
int DisplayStudent();
char StudentGrade(float);
float StudentAvg(int []);
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12
Student::Student(){
gptr = newint[3];
}
Student::Student(int size){
//How many grades would you like to insert?
int grade[size];
size = 3;
grade[size] = gptr[size];
}
First constructor is almost correct, Second one is wrong. You also need to save the size in the class (add int m_size in private section) so you can do bounds checking.
Also in your second constructor "int grade[size]" is on the stack, will be lost when method returns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Student::Student()
{
m_size = 3;
gptr = newint[3];
}
Student::Student(int size)
{
//How many grades would you like to save?
m_size = size;
gptr = newint[ size ];
}
Ohh, I see, thanks man. Allright, so now do I substitute all my grade arrays (which are 5 at the moment) to size and at one point ask the user how many grades would he like to enter?
Also a common variable naming format is the prefix all member variables with m_ and all method parameters without. On more complicated projects with methods many lines long it becomes obvious if a variable is a method parameter or a member variable, just my 2 cents worth. That's why I named the above m_size.
First you would ask the user how many grades they want to enter for a student.
Then create an instance of Student calling the second construtor with that number.
Then go into a for loop (0 to number user entered - 1) asking for each grade and calling StudentGrade method which would add it to gptr.
One thing I noticed is gptr is type int and StudentGrade takes a float, probably need to change gptr to float if 1/2 grades like 2.5, 3.5 are allowed.
Hmmm, my program keeps crashing, but I'm not quite sure why. I'll probably try debugging it, but here is what I have for the function of CreateStudent:
int Student::CreateStudent (){
// This function allows the user to insert as many students he/she wants
// to the class
// Variables required for the function
int age;
string name;
string last;
char gender;
int size;
int grade[size];
int j, i;
int students;
Student person;
//char fullname[25];
int Age;
char Gender;
int Grade;
fstream Class;
string ClassName;
cout << "Enter a class: " << endl;
cin >> ClassName;
if (CheckClass(ClassName)==true){
cout << "How many students would you like to enter: " << endl;
cin >> students;
for (i = 0; i < students; i++){
// Asks the user how many students he/she would like to enter
// and for each one it asks the user to enter the student record
cout << "Enter student information: " << endl;
cout << "Enter first name: " << endl;
cin >> name;
//strcpy(name, fullname);
cout << "Enter last name: " << endl;
cin >> last;
//strcpy(last, fullname);
cout << "Enter age: " << endl;
cin >> age;
//age = Age;
cout << "Enter gender: (M or F)" << endl;
cin > gender;
//gender = Gender;
cout << "How many grades would you like to enter? " << endl;
cin >> siz>e;
Student Student(size);
cout << "Now enter " << size << "grades:" << endl;
for (j = 0; j < size; j++){
cout << "Enter grade " << j + 1 << ": " << endl;
cin >> grade[j];
}
person.StoreStudent(ClassName, name, last, age, gender, grade); // Calls the function to store the student
}
return 0;
}
else {
cout << "Class does not exist. " << endl;
return 1;
}
}
The debugger should give you a call stack which points to the problem. If you need help, post a small, compilable example that demonstrates the problem and perhaps someone will help you. Since you are dealing with dynamic arrays I am guessing that there is something wrong with the way you are accessing and/or creating the array. You should probably read about the std::deque which is one of the built in dynamic sequence container class. It doesn't look like you have any requirement that says that you can't use the STL and it is part of the C++ language. http://cplusplus.com/reference/stl/deque/
By the way deque stands for double ended queue which means that you can insert values at either the beginning or end very efficiently. It has the operator[] so that you can access the values just as you would with a c-array but it also provides numerous other useful interface functions in addition to that. You can insert new values anywhere in the container during the program execution but insertion in the middle will cause things to be moved around. It is no big deal for your project since you may not need to resize it after creating it.
Moreover I do not know which compiler you are using but in C++ the values used to initialize an array must be constants at compile time. I see numerous examples in your code where you are using run-time values to create arrays at block scope which will not compile unless you have enabled one of your compiler extensions.