implementing classes' methods in Main()

Hey i have this Code (bottom) tbh i don't know what i'm doing wrong in implementing classes' methods really.. can someone fix this ?
also: before i tried running main i tested the methods/functions and always the program would run but the output would be "core dumped" and this"Course c1= new Course ("CSE",1,3,3);" should run but it gives me error , can you have a look on my code, thanks

#include <iostream>
#include <string.h>
using namespace std;

class Student{

char name[20];
int stno;
int ssn;
double gpa;
public:

Student(){ }

Student (char name[],int stno,int s, double g){

strcpy(this->name,name);
this->stno=stno;
this->ssn=s; //optional
this->gpa=g; //optional

}

char *getName(){
return name;
}

void setName(char n[]){
strcpy(name,n);
}

void setStno(int i){
stno=i;
}

int getStno(){
return stno;
}

void setSsn(int s){
ssn=s;
}

int getSsn(){
return ssn;
}

void setGPA(double g){
gpa=g;
}

double getGPA(){
return gpa;
}



};

class Course {
char cname[20];
int cno;
int CH;
Student *s;
int n;
public:

Course(char cname[],int cno,int CH, int n){

strcpy(this->cname,cname);

this->cno=cno;

s=new Student [n];

char sname[20]; int stno;int ssn; double gpa;

for (int i=1;i<=n;i++){

cout <<"Enter values for student #"<<i<<" Please enter name, stno,ssn, gpa: ";

cin>>sname>>stno>>ssn>>gpa;

s[i].setName(sname);
s[i].setStno(stno);
s[i].setSsn(ssn);
s[i].setGPA(gpa);

}
}

char *getName(){
return cname;
}

void setName(char n[]){
strcpy(cname,n);
}

void setCno(int c){
cno=c;
}

int getCno(){
return cno;
}

void setCH(int d){
CH=d;
}

int getCH(){
return CH;
}

Student * getStudent(){
return s;
}

void setStudent(char sname[], int sno,int ssn, double gpa){
delete s;
s = new Student(sname,sno,ssn,gpa);
}

bool IsEqual (Student student1,student2){
if(student1.gpa==student2.gpa && strcmp student1.sname(),student2.sname()==0)
return true;
return false;
}

Student *getStudentByName(char *n1){ // from previous lectrue
int c=0;
Student *ar;
for (int i=0;i<n;i++)
if(strcmp(s[i].getName(),n1)==0)
c++;
ar= new Student[c+1];
for (int i=0,j=0;i<n;i++)
if(strcmp(s[i].getName(),n1)==0){
ar[j]=s[i];
j++;
}
return ar;
}


Student *getStudentByGPA(double g) { // from previous lectrue

int c=0;

for (int i=0;i<n;i++)

if (s[i].getGPA()>=g)

c++;

Student *temp = new Student[c+1];

int j=0;

for (int i=0;i<n;i++)

if (s[i].getGPA()==g)

temp[j++]=s[i];

Student *ss=new Student("noname",0,0,-1);

temp[j]=*ss;

return temp;
}
};

int main()
{
Course c1= new Course ("CSE",1,3,3);
c1.getStudentByName("ahmad");
c1.getStudentByGPA(3.1);
return 0;
}


Last edited on
c1 isnt a pointer. and I am not sure why you would want it to be. just say
Course c1{"CSE",1,3,3};

<cstring> //C's strings, crude.
<string> //c++ strings, cleaner
<string.h> //C code, do not use in c++

Last edited on
Thank you it worked,i fixed some issues, but the problem is the code doesn't give an output sadly.. it gives me "segmentation fail"
i could share the task required if some1 is willing to help :|
Run it from inside your debugger, and it should show you where it crashed. Most likely an array index is out of bounds or a null pointer was used etc.

a hint:
--- what, exactly, is the value of 'n' in getStudentByName?
(print it at the start of the function, don't guess or give me words, tell me what the value IS). Perhaps, the Course constructor, maybe it needs this->n = n in it?
Last edited on
Not a total repair job but ...

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <string.h>

using namespace std;

class Student{

    char name[20];
    int stno;
    int ssn;
    double gpa;
public:

    Student(){ }

    Student (char name[],int stno,int s, double g){

        strcpy(this->name,name);
        this->stno=stno;
        this->ssn=s; //optional
        this->gpa=g; //optional

    }

    char *getName(){
        return name;
    }

    void setName(char n[]){
        strcpy(name,n);
    }

    void setStno(int i){
        stno=i;
    }

    int getStno(){
        return stno;
    }

    void setSsn(int s){
        ssn=s;
    }

    int getSsn(){
        return ssn;
    }

    void setGPA(double g){
        gpa=g;
    }

    double getGPA(){
        return gpa;
    }



};

class Course {
    char cname[20];
    int cno;
    int CH;
    Student *s;
    int n;
public:

    Course(char cname[],int cno,int CH, int n){

        strcpy(this->cname,cname);

        this->cno=cno;

        s=new Student [n];

        char sname[20]; int stno;int ssn; double gpa;

        for (int i=1;i<=n;i++){

            cout <<"Enter values for student #"<<i<<" Please enter name, stno,ssn, gpa: ";

            cin>>sname>>stno>>ssn>>gpa;

            s[i].setName(sname);
            s[i].setStno(stno);
            s[i].setSsn(ssn);
            s[i].setGPA(gpa);

        }
    }

    char *getName(){
        return cname;
    }

    void setName(char n[]){
        strcpy(cname,n);
    }

    void setCno(int c){
        cno=c;
    }

    int getCno(){
        return cno;
    }

    void setCH(int d){
        CH=d;
    }

    int getCH(){
        return CH;
    }

    Student * getStudent(){
        return s;
    }

    void setStudent(char sname[], int sno,int ssn, double gpa){
        delete s;
        s = new Student(sname,sno,ssn,gpa);
    }

    bool IsEqual (Student student1, Student student2){
        if(student1.getGPA() == student2.getGPA() &&
           strcmp( student1.getName(),student2.getName())==0 )
            return true;
        return false;
    }

    Student *getStudentByName(char *n1){ // from previous lectrue
        int c=0;
        Student *ar;
        for (int i=0;i<n;i++)
        if(strcmp(s[i].getName(),n1)==0)
            c++;
        ar= new Student[c+1];
        for (int i=0,j=0;i<n;i++)
        if(strcmp(s[i].getName(),n1)==0){
            ar[j]=s[i];
            j++;
        }
        return ar;
    }


    Student *getStudentByGPA(double g) { // from previous lectrue

        int c=0;

        for (int i=0;i<n;i++)

        if (s[i].getGPA()>=g)

            c++;

        Student *temp = new Student[c+1];

        int j=0;

        for (int i=0;i<n;i++)

        if (s[i].getGPA()==g)

            temp[j++]=s[i];

        Student *ss=new Student("noname",0,0,-1);

        temp[j]=*ss;

        return temp;
    }
};

int main()
{
    Course* c1= new Course ("CSE",1,3,3); 
    c1->getStudentByName("ahmad");
    c1->getStudentByGPA(3.1);
    return 0;
}


Enter values for student #1 Please enter name, stno,ssn, gpa: 

It would be better to use std::string and std::vector rather than char[] and new. However, consider:

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <cstring>
using namespace std;

class Student {
	char name[20] {};
	int stno {};
	int ssn {};
	double gpa {};

public:
	Course(const Course&) = delete;	// NOT YET IMPLEMENTED
	Course& operator=(const Course&) = delete; // NOT YET IMPLEMENTED

	Student() {}

	Student(const char* nam, int stn, int s, double g) : stno(stn), ssn(s), gpa(g) {
		strcpy(name, nam);
	}

	void setName(const char* n) { strcpy(name, n); }
	const char* getName() const { return name; }

	void setStno(int i) { stno = i; }
	int getStno() const { return stno; }

	void setSsn(int s) { ssn = s; }
	int getSsn() const { return ssn; }

	void setGPA(double g) { gpa = g; }
	double getGPA() const { return gpa; }

	bool IsEqual(const Student& student1) {
		return (student1.gpa == gpa) && strcmp(student1.name, name) == 0;
	}
};

class Course {
	char cname[20] {};
	int cno {};
	int CH {};
	Student *s {};
	int n {};

public:
	~Course() { delete[] s; }

	Course(const char* cnam, int cn, int CH1, int n1) : cno(cn), CH(CH1), n(n1) , s(new Student[n1]) {
		strcpy(cname, cnam);

		char sname[20] {};
		int stno {};
		int ssn {};
		double gpa {};

		for (int i = 0; i < n; ++i) {
			cout << "Enter values for student #" << i + 1 << " Please enter name (no space/tab) stno ssn gpa: ";
			cin >> sname >> stno >> ssn >> gpa;

			s[i].setName(sname);
			s[i].setStno(stno);
			s[i].setSsn(ssn);
			s[i].setGPA(gpa);
		}
	}

	void setName(const char* na) { strcpy(cname, na); }
	const char* getName() const {return cname; }

	void setCno(int c) { cno = c; }
	int getCno() const { return cno; }

	void setCH(int d) { CH = d; }
	int getCH() const { return CH; }

	/*
	// NO. s is a pointer to array, not single element
	const Student* getStudent() const {	return s; }
	void setStudent(const char sname[], int sno, int ssn, double gpa) {
		delete s;
		s = new Student(sname, sno, ssn, gpa);
	}
	*/

	// CALLER HAS TO DELETE RETURNED ALLOCATED MEMORY
	Student* getStudentByName(const char* n1) {
		int c {};

		for (int i = 0; i < n; ++i)
			if (strcmp(s[i].getName(), n1) == 0)
				++c;

		auto temp {new Student[c + 1]};
		int j {};

		for (int i = 0; i < n; ++i)
			if (strcmp(s[i].getName(), n1) == 0)
				temp[j++] = s[i];

		temp[j] = *new Student("noname", 0, 0, -1);
		return temp;
	}

	// CALLER HAS TO DELETE RETURNED ALLOCATED MEMORY
	Student* getStudentByGPA(double g) {
		int c {};

		for (int i = 0; i < n; ++i)
			if (s[i].getGPA() >= g)
				c++;

		auto temp {new Student[c + 1]};
		int j {};

		for (int i = 0; i < n; ++i)
			if (s[i].getGPA() >= g)
				temp[j++] = s[i];

		temp[j] = *new Student("noname", 0, 0, -1);
		return temp;
	}
};

void display(const Student* st)
{
	for (; strcmp(st->getName(), "noname") != 0; ++st)
		cout << st->getName() << ' ' << st->getStno() << ' '  << st->getSsn() << ' ' << st->getGPA() << ' ' << '\n';
}

int main()
{
	Course c1 {Course("CSE", 1, 3, 3)};

	auto bynam {c1.getStudentByName("ahmad")};
	auto bygpa {c1.getStudentByGPA(3.1)};

	cout << "\nBy name\n";
	display(bynam);

	cout << "\nBy gpa\n";
	display(bygpa);

	delete[] bynam;
	delete[] bygpa;
}



Enter values for student #1 Please enter name (no space/tab) stno ssn gpa: ahmad 1 1 3.1
Enter values for student #2 Please enter name (no space/tab) stno ssn gpa: momad 2 2 3.1
Enter values for student #3 Please enter name (no space/tab) stno ssn gpa: ahmad 3 3 3

By name
ahmad 1 1 3.1
ahmad 3 3 3

By gpa
ahmad 1 1 3.1
momad 2 2 3.1


Last edited on
In C++, an array of N items is indexed from 0 to N-1, not from 1 to N. So in your original post,
1
2
3
for (int i=1;i<=n;i++){

cout <<"Enter values for student #"<<i<<" Please enter name, stno,ssn, gpa: ";


should be
1
2
3
for (int i=0;i<n;i++){

cout <<"Enter values for student #"<<i<<" Please enter name, stno,ssn, gpa: ";

More comments.
Course::getStudentByName() returns an array of students, but there's no way to know how big it is.

Course::getStudentByGPA() counts students whose GPA is >= g, but copies students whose gpa is == g It should be consistent, but which one is correct? This is one reason why it's good to add a comment saying what the method should do.

There's no output because you don't generate any. main() should probably print the arrays returned by getStudentByName() and getStudentByGPA(). It should delete the arrays to free up the memory.

As others have said, these memory management issues will disappear if you use std::string and std::vector<> instead.
Topic archived. No new replies allowed.