C++ Programming Intro Course Class Program Problem

Write a class name StudentRecord with the following specifications –
• Data members:
- name, marks[ ], major, term (use appropriate data type)
• Member functions:
- You are allowed to leave the constructor default
- Implement all get/set methods
- double findAverage( ) – returns the average mark of the student
- void displayProfile( ) – displays the profile of a student in following manner
Name: John Smith
Major: Computer Science
Term: Fall 2015
Term average: 90%
- Hint: Use getline function to include space
• Main function:
- Take input from user for name, major and term name
- Take input from the user for 5 courses s/he has enrolled in this term (you must
put a validation checking that the range of input such that 100<=marks>=0)
- Create an object of StudentRecord class
- Set name, major, term name and the marks[]
- Invoke displayProfile() function to display the information of the student

Can someone check my coding and let me know of any errors? This is what I have so far:
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
#include <iostream>
using namespace std;

//defining a class name StudentRecord

class StudentRecord{

	//defining the characteristics of StudentRecord
	//Data members
	string name;
	int marks[];
	string major;
	string term;

	//defining the function of StudentRecord
	//Member functions

	public:
	//function prototypes
		StudentRecord();
		void setName();
		void setMarks(int mark);
		void setMajor();
		void setTerm();
		string getName();
		int getMarks();
		string getMajor();
		string getTerm();
		double findAverage();

double findAverage(){

}
void displayProfile(name, major, term, termAverage);
};

StudentRecord::StudentRecord(){
name = "John Smith";
major = "Computer Science";
term = "Fall 2015"
term average = 90
}

string StudentRecord::getName(){
return name;
}

string StudentRecord::getMarks(){
return marks;
}

string StudentRecord::getTerm(){
return term;
}

double StudentRecord::findAverage(){
return 0.0;
}

void StudentRecord::setName(string nm){
Name = nm;
}

void StudentRecord::setMajor(string mj){
Major = mj;
}

void StudentRecord::setTerm(string trm){
Term = trm;
}

void StudentRecord::setMarks(int mark){
Marks = mark;
}

void StudentRecord::setdisplayProfile(){

}

int main(){

StudentRecord myProfile;

string findName = myProfile.getName();
cout<< findName << endl;

string findMajor = myProfile.getMajor();
cout << findMajor <<endl;

string findTerm = myProfile.getTerm();
cout << findTerm << endl;

}
Last edited on
- Take input from user for name, major and term name


You didn't do that.
This is an update on the code I have written so far:
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
#include <iostream>
using namespace std;

//defining a class name StudentRecord

class StudentRecord{

	//defining the characteristics of StudentRecord
	//Data members
	string name;
	int marks[];
	string major;
	string term;

	//defining the function of StudentRecord
	//Member functions

	public:
		StudentRecord();
		void setName(string name);
		void setMarks(int marks);
		void setMajor(string major);
		void setTerm(string term);
		string getName();
		int getMarks();
		string getMajor();
		string getTerm();
		double findAverage();
		void displayProfile();
};

//Implementation of the functions
	
	//default constructor
	StudentRecord::StudentRecord(){
		cout << "constructor" << endl;
	}

	//defining mutators

	string StudentRecord::getName(){
		return name;
	}

	int StudentRecord::getMarks(){
		return marks;
	}

	string StudentRecord::getTerm(){
		return term;
	}

	//defining accesors

	void StudentRecord::setName(string name){
		
	}

	void StudentRecord::setMarks(int marks){
	
	}

	void StudentRecord::setMajor(string major){
		
	}

	void StudentRecord::setTerm(string term){
		
	}

	void StudentRecord::setdisplayProfile(){

	}

	//member function
	double StudentRecord::findAverage(){
		return 0.0;
	}


int main(){
	
	//taking input from user for name, major and term name
	int name, major, termname

	cout << "Please enter name: ";
	cin >> name;

	cout << "Please enter major: ";
	cin >> major;

	cout << "Please enter term name: ";
	cin >> termname;

	//taking input from user for 5 courses she/he has enroled in this term
	int course1, course2, course3, course4, course5;
	
	cout << "Please enter 1st course enrolled in: ";
	cin >> course1;
	
	cout << "Please enter 2nd course enrolled in: ";
	cin >> course2;

	cout << "Please enter 3rd course enrolled in: ";
	cin >> course3;

	cout << "Please enter 4th course enrolled in: ";
	cin >> course4;

	cout << "Please enter 5th course enrolled in: ";
	cin >> course5;
	//validation checking that the range of input such that 100<=marks>=0?

	//creating object of StudentRecord class?

	//setting name, major, term name and the marks[]?

	//invoking displayProfile() function to display the information of the student?

	return 0;
}
I see that you have created an int variable (i.e. a number) for the user to enter their name into. This doesn't seem to make much sense. If someone's name is, for example, "cowgirl", how can that be stored in an int?
Have you studied vectors? Ideally marks should be std::vector<unsigned> marks;. If you haven't studied vectors then make it unsigned marks[5];

Enter the 5 courses in a loop and store the results in an array:
1
2
3
4
5
6
unsigned courses[5];
for (int i=0; i<5; ++i) {
    cout << "Enter grade (0-100) for course #" << i+1 << ": " << flush;
    cin << courses[i];
    // TODO: once the code works, add error checking here.
}

updated written 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
120
121
122
123
124
125
126
#include <iostream>
using namespace std;

const int NUM_COURSES = 5;

//defining a class name StudentRecord

class StudentRecord{

	//defining the characteristics of StudentRecord
	//Data members
	string name;
	double marks[NUM_COURSES];
	string major;
	string term;

	//defining the function of StudentRecord
	//Member functions

	public:
		StudentRecord(); //default constructor
		void setName(string name);
		void setMarks(double marks[NUM_COURSES]);
		void setMajor(string major);
		void setTerm(string term);
		string getName();
		double getMarks();
		string getMajor();
		string getTerm();
		double findAverage();
		void displayProfile();

	~StudentRecord(){
		cout << "destructor" << endl;
	}
};

//Implementation of the functions
	
	//default constructor
	StudentRecord::StudentRecord(){
		cout << "constructor" << endl;
	}

	//defining mutators

	string StudentRecord::getName(){
		return name;
	}

	double StudentRecord::getMarks(){
		return marks[NUM_COURSES];
	}

	string StudentRecord::getMajor(){
		return major;
	}

	string StudentRecord::getTerm(){
		return term;
	}

	double StudentRecord::findAverage(){
		return 0.0;
	}

	//defining accesors

	void StudentRecord::setName(string n){
		name = n;
	}

	void StudentRecord::setMarks(double m){
		marks[NUM_COURSES] = m;
	}

	void StudentRecord::setMajor(string maj){
		major = maj;
	}

	void StudentRecord::setTerm(string t){
		term = t;
	}


int main(){
	
	//taking input from user for name, major and term name
	string name, major, term;

	cout << "Please enter name: ";
	cin >> name;

	cout << "Please enter major: ";
	cin >> major;

	cout << "Please enter term name: ";
	cin >> term;

	//taking input from user for marks for 5 courses she/he has enroled in this term marks
	int course1, course2, course3, course4, course5;
	
	cout << "Please enter 1st enrolled in course mark: ";
	cin >> course1;
	
	cout << "Please enter 2nd enrolled in course mark: ";
	cin >> course2;

	cout << "Please enter 3rd enrolled in course mark: ";
	cin >> course3;

	cout << "Please enter 4th enrolled in course mark: ";
	cin >> course4;

	cout << "Please enter 5th enrolled in course: ";
	cin >> course5;
	//validation checking that the range of input such that 100<=marks>=0?

	//creating object of StudentRecord class?

	//setting name, major, term name and the marks[]?

	//invoking displayProfile() function to display the information of the student?

	return 0;
}

Please, check for errors and give me hints on how to the things I haven't written code for in the main function.
(you must put a validation checking that the range of input such that 100<=marks>=0)


That's the next piece. I suggest you look into loops for this. You need to loop each input until the user has entered an acceptable value.
1. the #include <string> is missing

2. if you really want to use an array for marks instead of a std::vector, the constructor should initialize it:
1
2
3
4
5
StudentRecord::StudentRecord() 
	: marks()
{
	cout << "constructor" << endl;
}


3. the implementation of setMarks differs from its declaration. However, both are wrong, as well as getMarks. It should be:
1
2
3
4
5
6
7
8
9
double StudentRecord::getMark(int index) const
{
	return marks[index];
}

void StudentRecord::setMark(int index, double value)
{
	marks[index] = value;
}
note also the const on getMark, to be used normally for getters. In your case all your getters should be const, as well as findAverage and probably displayProfile
If you want input for 5 integers, simply use a cleaner for loop:
1
2
3
4
5
6
7
int mark = 0;
for(int i = 0; i < num_courses; ++i)
{
     cin>>mark;
     //Check mark validity
     student.setMark(i, mark) //using fcantoros implementation
}
Topic archived. No new replies allowed.