don't know why it is not running

I am trying to use structures while getting the grades of students and stating the number of students that passed a particular score after accepting a certain number

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
// eXercise.cpp : Defines the entry point for the console application.
//

#include "stdio.h"
#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;


class passMark {

	struct studentresult {
		float Phy_Marks, Chem_Marks, Maths_Marks;
	}studentarray[50];
	int i, j = 0, k = 0;
	
public:
	void acceptData() {
		for (i = 0; i < 50; i++) {
			cout << "Please enter the scores of the students in the Chemistry class.";
			cin >> studentarray[i].Chem_Marks;
			cout << endl << "Please enter the scores of the students in the Mathematics class.";
			cin >> studentarray[i].Maths_Marks;
			cout << endl << "Please enter the scores of the student in the Physics class.";
			cin >> studentarray[i].Phy_Marks;
		}

	}
	void chemStudent() {
		
		for (i = 0; i < 50; i++) {
			if (studentarray[i].Chem_Marks >= 70) {
				j++;
				}
			else if (studentarray[i].Chem_Marks <= 35){
				k++;
			}
			}
		}
	void mathStudent() {

		for (i = 0; i < 50; i++) {
			if (studentarray[i].Maths_Marks >= 70) {
				j++;
			}
			else if (studentarray[i].Maths_Marks <= 35) {
				k++;
			}
		}
		}
	void phyStudent() {

		for (i = 0; i < 50; i++) {
			if (studentarray[i].Phy_Marks >= 70) {
				j++;
			}
			else if (studentarray[i].Phy_Marks <= 35) {
				k++;
			}
		}
	}

};

int main()
{
	passMark aise;
	cout << aise.chemStudent.j << " students passed Chemistry with distinction.";
	cout << aise.chemStudent.k << " students failed Chemistry.";
	cout << aise.mathStudent.j << " students passed Mathematics with distinction.";
	cout << aise.mathStudent.k << " students failed Mathemics.";
	cout << aise.phyStudent.j << " students passed Physics with distinction.";
	cout << aise.phyStudent.k << " students failed Physics.";

	system("pause");
	}

I'll advice you to use functional programming in this type of problems but if it is necessary that you OOP then, make your classes meaningful. For example, you could have a student class and there you can have the grades of the student as it's data. You can also have a course class that has an array of students as its data. This way, your code will be easier to comprehend.

But working with your code, make the acceptdata() function a constructor so that the score of all the students will be entered as the object is created. (Although, this is bad programming practice as the number of students can change and it's bad for modularity).
Lastly, stop including libraries that you do not need in your code.

I hope I helped in someway. If I have some time today, I'll try to write the program.
Last edited on
Is this correct as a "Student" class?

Student.h
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
#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>

using namespace std;

class Student {
	//Overloading streams
	friend ostream &operator<<(ostream &, const Student &);
	friend istream &operator>>(istream &, Student &);
	
	private:
		float chem;
		float math;
		float phys;
	
	public:
		//Constructors
		Student(const float chem = 0, const float math = 0, const float phys = 0);
		Student(const Student &);
		
		//Get and Set functions
		float getStudentChem() const {return chem;}
		float getStudentMath() const {return math;}
		float getStudentPhys() const {return phys;}
		void setStudentChem(const float C) {chem = C;}
		void setStudentMath(const float M) {math = M;}
		void setStudentPhys(const float P) {phys = P;}
		
		//Deconstructor
		~Student();
};

#endif 


Student.cpp
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
#include <iostream>
#include "Student.h"

Student::Student (const float C, const float M, const float P) {
	chem = C;
	math = M;
	phys = P;
}

Student::Student (const Student & S) {
	chem = S.chem;
	math = S.math;
	phys = S.phys;
}

ostream & operator<<(ostream & os, const Student & S) {
	os << "Chemistry: " << S.chem << "\nMathematics: " << S.math << "\nPhysics: " << S.phys <<endl;
	return os;
}

istream & operator>>(istream & is, Student & S) {
	cout << "Insert Chemistry score: ";
	is >> S.chem;
	cout << "Insert Mathematics score: ";
	is >> S.math;
	cout << "Insert Physics score: ";
	is >> S.phys;
	return is;
}


After this how should the "Course" class be like? A list, a queue or a stack?
I hope this helps
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
#include <iostream>

using namespace std;

struct Marks	{
	/*If this function is kept in the class, you can't see it outside the scope of the class*/
		float chem_mark=0;    
		float phy_mark=0;
		float math_mark=0;
};

class Student
{
	private: 
		Marks mymark;
		string name;
		
	public:		
		void set()
		{
			cout<<"Student name: ";
			cin>>name;
			if(name=="exit")
				return ;
			cout<<"Chemistry mark: ";
			cin>>mymark.chem_mark;
			cout<<"Physics mark: ";
			cin>>mymark.phy_mark;
			cout<<"Maths mark: ";
			cin>>mymark.math_mark; cout<<endl;
		}
		
		string getname()
		{
			return name;
		}
		
		float getchemmark()
		{
			return mymark.chem_mark;
		}
		
		float getphymark()
		{
			return mymark.phy_mark;
		}
		
		float getmathmark()
		{
			return mymark.math_mark;
		}
		
		
};

class passmark
{
	private: 
		Student students[100]; int num_of_students;
		int c=0, p=0, m=0;
		
	public:
		passmark()
		{
			num_of_students=0;
			while(1)
			{
				students[num_of_students].set();
				if(students[num_of_students].getname() == "exit")
					break;
				else
					num_of_students++;
			}
		}
		
	/*	int  chemstud(int k) //pass 0 to the function and it will return number of people that failed.. 
		{			//pass non zero int and it will return number of people that passed chemistry(above 70)
			int num=0;
			for(int b=0;b<num_of_students;b++)
				if(students[b].getchemmark() >= 70)
					num++;						
			
			if(k==0)
				return num;
			else
				return (num_of_students-num);
		}
		
		
		int  mathstud(int k) //0 for fail... 1 for pass
		{
			int num=0;
			for(int b=0;b<num_of_students;b++)
				if(students[b].getmathmark() >= 70)
					num++;						
			
			if(k==0)
				return num;
			else
				return (num_of_students-num);
		}
		int  phystud(int k) //0 for fail... 1 for pass
		{
			int num=0;
			for(int b=0;b<num_of_students;b++)
				if(students[b].getphymark() >= 70)
					num++;						
			
			if(k==0)
				return num;
			else
				return (num_of_students-num);
		}
			These functions return the number of people that passed a particular course
			It is better to write just one function that returns the number of people that passed each course (a structure) as
			as I did later.
		*/
		
		int numberofstuds()
		{
			return num_of_students;
		}
		
		Marks pass_fail()	{
			Marks abcd; // this will hold the number of people that passed each of the courses
			for(int b=0; b<num_of_students; b++)
			{
				if(students[b].getchemmark() >= 70)
					abcd.chem_mark+=1;
				if(students[b].getmathmark() >= 70)
					abcd.math_mark+=1;
				if(students[b].getphymark() >= 70)
					abcd.phy_mark+=1;
			}
			return abcd;
				
		}
		
};


int main()
{
	passmark aise;
	
	Marks abcd;
	abcd=aise.pass_fail();
	
	
	cout<<abcd.chem_mark<<" passed chemisty"<<endl;
	cout<<abcd.math_mark<<" passed math"<<endl;
	cout<<abcd.phy_mark<<" passed physics"<<endl;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.