c++ dynamic classes without using vector - c++

So I happened to finish my homework program but today during lecture my good ol professor told us we are not allowed to use STL as in vectors or list to create our database. We were also told we need all our variables private. I was doing this program completely wrong. This is what I have so far.
in class.h
class Student {
private:
string last;
string first;
int student_id;
int enroll_id;
int *grades;
}
class Gradebook {
public:
Gradebook();
void newCourse(Gradebook *info);
private:
string name;
int course_id;
int count_course;
int enroll;
Student *students;
public:
//Constructor
}
I know I can access private members of Gradebook by using a constructor so I can set every member in my Gradebook.
function to create a newCourse
Gradebook::Gradebook() {
students = new Student;
course_id=0;
count_course=0;
enroll = 0;
}
Gradebook::newCourse(Gradebook *info) {
int i, loop=0;
cout << "Enter Number of Courses: ";
cin >> loop;
info = new Gradebook[loop];
for(i=0; i<loop; i++) {
cout << "Enter Course ID: ";
cin >> info[info->count_course].course_id;
cout << "Enter Course Name: ";
cin >> info[info->count_course].name;
info->count_course++
}
}
So Courses are now sets. Since the variables in Student are private. I can't just use a pointer to the variables to access them. Can someone show me how to do this?

Ok I didnt know how to ask this question but I actually answered it. But I want everyones opinion on my method.
class Student {
public:
void setID(int ID){student_id = ID; };
int getID(){return student_id);
private:
string last;
string first;
int student_id;
int enroll_id;
int *grades;
};
class Gradebook {
public:
Gradebook();
void newCourse(Gradebook *info);
void newStudent(Gradebook *info);
private:
string name;
int course_id;
int count_course;
int count_student;
int enroll;
Student *students;
public:
//Constructor
}
void Gradebook::newStudent() {
int i, loop=0;
int student=0;
string lastName;
cout << "Enter number of students: ";
cin >> loop;
for(i=0; i<loop; i++) {
cout << "Enter Student ID: ";
cin >> student;
info->students[info->count_student].setID(student);
cout << "Enter Last Name: ";
cin >> lastName;
info->students[info->count_student].setLast(lastName);
info->count_student++;
}
}
Is there anything wrong of doing it this way?

edit: You can't use 'Student *students' for your container for multiple Students...
You could use your own lists. Something like
struct Participant{ // or class (and maybe more clever name)
Student *student;
Participant *prev;
Participant *next;
};
You have to do little pointer-acrobatics, but maybe that's the idea for this exercise..
And like in previous answer, use get and set functions in your Student class.

Ok, I'm sorry but your code is a mess...
I can't do this homework for you, but here is few tips that came in mind
Are you sure that it wouldn't be easier to make different classes for Student, Course and Gradebook? I don't know your homework specifications, so I can't be sure what it is that your program should actually do.
You can not use int *grades to store all of one students grades. Same goes for Student *students. You can not access iterate *students like an array students[i].something()
If you use some help class(or struct) like Participant, you have to find right student by iterating in loop. Notice, that you have store 'first' participant inside your class and 'int students' keep in your student count (also inside your class)
Participant *current = first;
int id = Id; // Id is from function/method call
for(unsigned int i = 0; i < students; ++i){
if(current->getId()== id){
// do something and
break;
}
else if(current->next != NULL){ // test this just in case
current = current->next;
}
else break; // something went wrong
}
It might be good idea to store all students in one list (that you make) and use pointers in Course or in Gradebook or where ever... If you know 'static' here is a hint
class Student{
public:
. // get and set functions
static Student* getStudent(int id); // return NULL if no student with id
private:
static Student *first;
static Student *last;
static unsigned int counter; // helps with loops, but you can always use next != NULL
Student *next;
Student *prev;
}
Put this inside the Student constructor
with first student that you create you set first and last to point him/her, and next and prev to NULL. After that, always when you create a new student you set
this->prev = last;
last->next = this;
last = this;
this->next = NULL;
Do not implement program logic inside class. Do it in main() or in some other function, but i think main() is fine for now. If you need to add new student in gradebook, ask all the necessary info in main() and make some function like Gradebook::newStudent(int &Id, string &last_name){ // store student info in gradebook }
Usually these homework programming exercises don't have be too fancy or streamlined or fully optimized. So don't overdo them ;)

class Student {
private:
string last;
string first;
int student_id;
int enroll_id;
int *grades;
public:
string &getLast(){ return Last; }
...
...
};
Call Student::getLast() when you need to access your last variable etc
or
void setLast(string sLast){last = sLast;} for writing and
string getLast(){return last;} for reading
And example of dynamic array:
struct Student;
int Count = 0;
Student *Students = nullptr;
int AddStudent(Student nStudent)
{
Student *Buffer = new Student[Count + 1];
for (int a = 0; a < Count; a++)
Buffer[a] = Student[a];
Buffer[Count] = nStudent;
if(Students)
delete[] Students;
Students = Buffer;
return ++Count -1
}
void RemoveStudent(int Index)
{
Student *Buffer = new Student[Count - 1];
for (int a = 0; a < Index; a++)
Buffer[a] = Students[a];
for (int a = Index; Index < Count - 1; a++)
Buffer[a] = Students[a - 1];
if (Students)
delete[] Students;
Students = Buffer;
}

Related

Nested structure function in c++

I'm having trouble with one of my assignments (or maybe I'm overthinking it?)
I need to create
a function to take integer parameters for number of students and tests.
Allocate the memory needed for the array of students and the array of test scores for each student.
Return a pointer to the array of Student structures. No display output is done in this function.
int main()
{
int studentNum;
int testNum;
cout << "How many students are there?\n";
cin >> studentNum;
cout << "How many tests are there?";
cin >> testNum;
system("pause");
return 0;
}
my function
GradeBook *initArrays(int s, int t)
{
GradeBook *ptr;
// Allocate the array of Student structures.
ptr = new GradeBook[s];
// Allocate an array of ints (to hold test scores)
// for each element of the array of Student structures.
for (int count = 0; count < s; count++)
{
ptr[count].tests = new int[t];
}
// Return a pointer to the array of structures.
return ptr;
}
edit: I have edited my function, could i get some opinions on that?
if you are writing this in c++, use classes. if i understand correctly, you should create a structure to save a students id,name,or something and a corresponding grade?
something like:
class Test{
public:
int id;
int grade;
Test(int id, int grade){
this->id = id;
this->grade = grade;
}
};
class Student{
public:
int id;
std::string name;
std::vector<Test> tests;
Student(int id, std::string name)
{
this->id = id;
this->name = name;
}
};
int main(){
vector<Student> students;
int studentnum;
for (int i = 0; i < studentnum; i++){
students.push_back(Student(i, "name"));
//insert all tests of the student by calling students[i].tests.push_back(Test(id, grade))
}
}
this way you don't have to allocate memory, which you can easily overlook freeing.
edit:
this is very basic and not a sophisticated solution, as the properties of the classes are all public.
edit 2:
typedef struct Test{
int id;
int grade;
}Test;
typedef struct Student{
int id;
std::string name;
Test * tests;
}Student;
int main(){
Student * students;
int studentnum;
students = (Student*)malloc(sizeof(Student)*studentnum);
for (int i = 0; i < studentnum; i++){
students[i]->id = id;
students[i]->name = "name";
student[i]->tests = (Test*)malloc(sizeof(Test)*numberofgradesofthatstudent);
for (int j = 0; j < numberofgradesofthatstudent; j++)
{
students[i]->tests[j]->id = testid;
students[i]->tests[j]->grade = grade;
}
}
}
this is schematic! new and malloc reserve memory on the heap, do not forget to free everything when you are done with it.
As said a little above, be careful using brackets {} to delimit your blocks.
Secondly,the syntax:
array[studIndex].Tests
supposes that the value array[studIndex] (here an integer) has a member value named Tests. But in this case it doesn't.
Think about your problem: you need to store two values "connected" to one another in a static array. The way I see it, you should try on with two dimensional arrays:
int 2dArray[nbStudents][nbTests];
If you don't want to bother with 2dimensional arrays, you can also try
int 2dArray[nbStudents * nbTests];
But for conveniance, it is often better to use 2d arrays.
Also, think about declaring your array before the for loops in your function.
Then concatenate two for loops as you did and I'll let you think about the rest...

Segmentation fault with dynamically allocated arrays as members of structure in c++?

I am writing a program using an array of structures to store a name, id number, and an array of test scores for a certain amount of students. Both the array of structures and the array of test scores member need to be dynamically allocated. I've gotten down to the function that allows the user to input test scores for each student, however I am having problems with the cin in the last function (getScores function). When using Linux I get a segmentation fault, so I'm assuming it has something to do with the dynamically allocated tests array that is a member of the structure, I just can't see it. I'm wondering how I can go about debugging it and an explanation of why this is actually occurring so I can avoid it in the future.
//Preprocessor Directives
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Structure declaration
struct Students
{
string name; //Student name
int idNum; //Student ID number
double *tests; //Pointer to an array of test scores
};
//Function prototypes
Students *getStudents(int &);
double *getTests(int &);
void getInfo(string &, int &, int);
void getScores(double &, string, int);
//Main program section
int main ()
{
int numStudents = 0;
int numTests = 0;
Students *studentFiles = NULL;
//Call the getStudents function
studentFiles = getStudents(numStudents);
//Call the getTests function
studentFiles->tests = getTests(numTests);
for(int i = 0; i < numStudents; i++)
{
//Call the getInfo function
getInfo(studentFiles[i].name, studentFiles[i].idNum, i+1);
}
for(int i = 0; i < numStudents; i++)
{
for(int j = 0; j < numTests; j++)
{
getScores(studentFiles[i].tests[j], studentFiles[i].name, j);
}
}
delete [] studentFiles;
delete [] studentFiels->tests;
return 0;
}
Students *getStudents(int &numStudents)
{
Students *studentFiles = NULL;
//Prompt the user for the number of students
cout<<"Enter the number of students: ";
cin>>numStudents;
//Dynamically allocate an array of structs, one for each student
studentFiles = new Students[numStudents];
return studentFiles;
}
double *getTests(int &numTests)
{
double *tests = NULL;
//Prompt the user for the number of tests
cout<<"Enter the number of tests: ";
cin>>numTests;
cin.ignore();
//Dynamicall allocate an array of integers, one for each test
tests = new double[numTests];
return tests;
}
void getInfo(string &name, int &idNum, int index)
{
//Prompt for each student's name and id number
cout<<"Enter the name of student #"<<index<<": ";
getline(cin, name);
cout<<"Enter the id number of student #"<<index<<": ";
cin>>idNum;
cin.ignore();
}
void getScores(double &test, string name, int numTest)
{
cout<<name<<endl;
cout<<numTest<<endl;
//Prompt for each test score for each student
cout<<"Enter "<<name<<"'s score for test #"<<numTest+1<<": ";
cin>>test;
}
One error is that you access a member of deleted object studentFiles. Reverse the lines to fix that:
delete [] studentFiles->tests;
delete [] studentFiles;
Ideally, use std::vector<> instead of dynamically allocating and releasing memory manually.
Also note, that the code only initializes Student::tests of the first member of the array, the rest of Student objects have this member uninitialized. The result of expression studentFiles[i].tests[j] is undefined and is likely to cause a crash.
You need to initialize Student::tests member of each Student. And when done, deallocate Student::tests of each Student.

Linked list of pointers C++

I have a list, but now I have to link it.
Here is my program ( I deleted code inside functions, to make my program more easy to read ).
#include <iostream>
using namespace std;
struct Student
{
char ime[16];
char priimek[16];
char vpisna[10];
char ocenaRV[10];
char ocenaDN[10];
char ocenaKV[10];
char ocenaVI[10];
Student *next;
};
void clean(Student* pointer,int x) // Delete random data
{
}
void dodajanje(int x,Student* s) // Add information about student
{
}
void brisi(Student* pointer,int x) // Delete information about student
{
}
int main()
{
int student,mesto, brisanje, ali = 0;
cout << "Number of students?." << endl;
cin >> student;
Student* s = new Student[student];
clean(s,student);
cout << endl;
cout << "Add student to i place in array." << endl;
cin >> mesto;
dodajanje( mesto, s );
for(int i=0;i<(student*2);i++)
{
cout << "add student = 1, delete student = 2, cout information = 3"<<endl;
cin>>ali;
if (ali == 1)
{
cout << endl;
cout << "Add student to i place in array." << endl;
cin >> mesto;
dodajanje( mesto, s );
}
else if (ali == 2)
{
cout << "delete student on i place ?" << endl;
cin >> brisanje;
brisi(s,brisanje);
}
else
{
break;
}
}
delete[] s;
return 0;
}
Can someone explain me how to link my list, because code in all tutorials I came across was similar to this:
Node* temp = Node();
But in my program my code is:
Student* s = new Student[student];
And now I'm lost;
Note: I have to create dynamically linked list.
Node* temp = Node();
This creates a single Node instance. Though it should be this instead:
Node* temp = new Node;
Student* s = new Student[student];
This creates an array of student number of Student instances. This defeats the purpose of a linked list , as you won't be able to add/remove Student instances from the array efficiently. But, just for the sake of argument, lets say you really need an array. You can "link" the Student instances together like this:
for (int i = 0; i < (student-1); i++)
s[i].next = &s[i+1];
s[student-1].next = NULL;
If you actually need a linked list then you need something more like this instead:
Student *studentList = NULL;
Student *lastStudent = NULL;
for (int i = 0; i < student; ++i)
{
Student* s = new Student;
s->next = NULL;
if (lastStudent) lastStudent->next = s;
if (!studentList) studentList = s;
lastStudent = s;
}
// use studentList as needed...
Student *s = studentList;
while (s)
{
Student *next = s->next;
delete s;
s = next;
}
After fixing that, consider using the STL std::list class instead, or even std::forward_list in C++11.
That being said, you also need to rethink your code design. A linked list grows and shrinks dynamically, so there is no point in asking the user for the number of students up front, or pre-allocating the list with garbage that has to be cleaned before it can be used. Change your loop to run forever (or at least until the user says to stop). On each iteration, ask the user what to do. If Add, add a new Student to the list at that time. If Delete, ask the user which student to delete, find that Student, unlink it, and delete it. If Display, ask the user which student to display, find that Student, and display it. And so on.
Linked list is a node based data structure. What you are trying to do is creating a dynamic array of students not a linked list.
If you really need to create a linked list, in place of following line
Student* s = new Student[student];
you should create a nodes as follows in number of time of students in a for loop and link each other by updating student()-> next= next_student (Psuedo code)
Student* s = new Student;
And at end, you have to call delete s within a for loop to deallocate the memory.

How to create a pointer to pointer array to struct?

I want to create a dynamic array of pointers that each one of them points to a struct. In the program there is an option to add structs and if the counter reaches the last the value of the array, the array expands.
struct student
{
string id;
string name;
};
int N=5;
int counter=0;
student **big=new student *[N]; //a ptr to an array of ptr's.
void add_student (int &counter,student **big)
{
int i;
if (counter==0)
{
for (i=0; i<N; i++)
{
big[i]=new student;
}
}
if (counter==N)
{
N+=5;
student **temp=new student *[N];
for (i=counter-1; i<N; i++)
{
temp[i]=new student;
}
for (i=0; i<counter; i++)
{
temp[i]=big[i];
}
delete [] big;
big=temp;
}
cout<<"Enter student ID: "<<endl;
cin>>(*big)[counter].id;
cout<<"Enter student name: "<<endl;
cin>>(*big)[counter].name;
counter++;
}
When I run the program it crashes after I try to add more than one student. Thanks!
Try this code. Main problem was that you write to (*big)[counter].id without this being valid memory. In my function below a student object is created first, then writen to.
PS: I have not tested the code, let me know if it has problems with it.
struct student {
string id;
string name;
};
int N=5;
int counter=0;
student **big = new student *[N]; //a ptr to an array of ptr's.
// Variable big and counter is global, no need to pass as argument.
void add_student (student *new_student) {
// Resize if needed
if (counter==N) {
int i;
student **temp=new student *[N+5];
// Copy from the old array to the new
for (i=0; i<N; i++) {
temp[i]=big[i];
}
// Increase maximum size
N+=5;
// Delete the old
delete [] big;
big=temp;
}
// Add the new student
big[counter] = new_student;
counter++;
}
// Function called when we should read a student
void read_student() {
student *new_student = new student;
cout<<"Enter student ID: "<<endl;
cin>>new_student->id;
cout<<"Enter student name: "<<endl;
cin>>new_student->name;
// Call the add function
add_student (new_student);
}
I just tried. the error is because you are not handling pointer to pointer to a struct correctly. to pass a pointer to pointer to something means the function can change the pointer address not only the thing it points to. so declaring a pointer to pointer in function is okay but declaring a global p to p to s doesn't make much sense. You can achieve the same effect using &ptr. for p to p to s i.e. pass the address of pointer to function. I made some changes but I'm not sure if it works. I will try again after 4/5 hours and will check the issue in detail. for the time being please content yourself with the following. (might be some errors below so be careful)
struct student
{
string id;
string name;
};
int N=5;
int counter=0;
student *big=new student[N]; //a ptr to an array of ptr's.
void add_student (int &counter,student **ppBig)
{
int i;
if (counter==0)
{
for (i=0; i<N; i++)
*ppBig[i]=new student;
}
if (counter==N)
{
N+=5;
student *temp=new student [N];
for (i=counter-1; i<N; i++)
temp[i]=new student;
for (i=0; i<counter; i++)
temp[i]=*ppBig[i];
delete[] *ppBig;
ppBig=temp;
}
cout<<"Enter student ID: "<<endl;
cin>>(*big)[counter].id;
cout<<"Enter student name: "<<endl;
cin>>(*big)[counter].name;
counter++;
}

windows causing breaking point

I am writing a program for a class assignment on learning about pointers and memory management. Allocating memory for student and courses is necessary with the new operator in this assignment and the program seems to run properly but after it prints everything the program crashes and it says that that it has triggered a break point. Any help would be appreciated.
The error is located here but I'm not sure what it means
/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Student //Structs
{
string firstName, lastName, aNumber;
double GPA;
};
struct Course
{
int courseNumber, creditHours;
string courseName;
char grade;
};
Student* readStudent(); // Function Prototypes
Course* readCourseArray(int&);
void computeGPA(Student *student, Course *courseArray, int coursesTaken);
void printSummary(Student *studentPTR, Course *courseArray, int courses);
int main() //Main
{
int courses = 0;
Student *studentPTR = readStudent();
Course *coursePTR = readCourseArray(courses);
computeGPA(studentPTR, coursePTR, courses);
printSummary(studentPTR, coursePTR, courses);
delete studentPTR;
delete coursePTR;
system ("pause");
return 0;
}
Student* readStudent() // Read Student
{
Student* student = new Student;
cout<<"\nEnter students first name\n";
cin>>student->firstName;
cout<<"\nEnter students last name\n";
cin>>student->lastName;
cout<<"\nEnter students A-Number\n";
cin>>student->aNumber;
return student;
}
Course* readCourseArray(int &courses) //Read Courses
{
cout<<"\nHow many courses is the student taking?\n";
cin>>courses;
const int *sizePTR = &courses;
Course *coursePTR = new Course[*sizePTR];
for(int count = 0; count < *sizePTR; count++) //Enter course information
{
cout<<"\nEnter student "<<count+1<<"'s course name\n";
cin>>coursePTR[count].courseName;
cout<<"\nEnter student "<<count+1<<"'s course number\n";
cin>>coursePTR[count].courseNumber;
cout<<"\nEnter student "<<count+1<<"'s credit hours\n";
cin>>coursePTR[count].creditHours;
cout<<"\nEnter student "<<count+1<<"'s grade\n";
cin>>coursePTR[count].grade;
}
return coursePTR;
}
void computeGPA(Student *studentPTR, Course *courseArray, int coursesTaken) // Compute GPA
{
double total = 0, hours = 0;
for(int count = 0; count < coursesTaken; count++)
{
hours += courseArray[count].creditHours;
if (courseArray[count].grade == 'A')
total+=4;
else if (courseArray[count].grade == 'B')
total+=3;
else if (courseArray[count].grade == 'C')
total+=2;
else if (courseArray[count].grade == 'D')
total+=1;
else if (courseArray[count].grade == 'F')
total+=0;
}
studentPTR->GPA = (total / hours);
}
void printSummary(Student *studentPTR, Course *courseArray, int courses)
{
cout<<"\nReport\n";
cout<<"\nNumber Name Hours Letter Grade Point Grade";
cout<<"\n------ ---- ----- ------------ -----------\n";
for(int count = 0; count < courses; count++)
cout<<setw(3)<<courseArray[count].courseNumber<<" "<<courseArray[count].courseName<<setw(20)<<" "<<courseArray[count].creditHours<<setw(5)<<" "<<courseArray[count].grade<<endl;
cout<<"\nStudent :"<<studentPTR->firstName<<" "<<studentPTR->lastName;
cout<<"\nA-Number :"<<studentPTR->aNumber;
cout<<"\nOverall GPA :"<<studentPTR->GPA;
}
You're allocating courses with operator new[] (which is for arrays), but freeing it with operator delete, instead of operator delete[].
You should deallocate it like that:
delete[] coursePTR;
I also don't understand why you use the sizePTR pointer instead of directly using courses.
BTW, just so you'd know, this is not a 'find my bug' site. You have a code review site for that.
You need to change
delete coursePTR;
to
delete [] coursePTR;
new always needs to be used with delete, and new [] with delete [], otherwise bad things happen.