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
// eXercise.cpp : Defines the entry point for the console application.
//
#include "stdio.h"
#include "stdafx.h"
#include "iostream"
#include "conio.h"
usingnamespace 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++;
}
elseif (studentarray[i].Chem_Marks <= 35){
k++;
}
}
}
void mathStudent() {
for (i = 0; i < 50; i++) {
if (studentarray[i].Maths_Marks >= 70) {
j++;
}
elseif (studentarray[i].Maths_Marks <= 35) {
k++;
}
}
}
void phyStudent() {
for (i = 0; i < 50; i++) {
if (studentarray[i].Phy_Marks >= 70) {
j++;
}
elseif (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.
#include <iostream>
usingnamespace 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;
}