Want to change something. Idk how.

I want to make it so if someone has 0 absences, it adds 2 points to their "Grade". How would I go about doing this? Here is my 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
#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;
class student
{
private :
	int sno;
	char name[20];
	int g1,g2,g3;
	int absen;
public :
	void Getdata()
	{
		cout << "Enter Student ID:  "; cin >> sno;
		cout << "Enter Student NAME:  "; cin >> name;
		cout << "Enter Student TEST SCORES (3):  "; cin >> g1 >> g2 >> g3;
		cout << "Enter # of Absences:  "; cin >> absen;
	}
	void Putdata()
	{
		cout  << "Student ID:  " << sno << endl;
		cout << "Student NAME:  " << name << endl;
		cout << "# of Absences:  " << absen << endl;
		cout << "Grade:  " << (g1+g2+g3)/3 << endl;
		if ((((g1+g2+g3)/3) >= 73))
		{cout << "Successful" << endl;}
		else
		{cout << "Unsuccessful" << endl;}
	};
};


int main()
{
	student s;
	s.Getdata();
	s.Putdata();
	getch();
	return 0;
}
Lots of ways you could do this. You could use another grade variable, and then:

1
2
3
4
5
6
7
8
if (absen == 0) {
  g4 = 2;
} else {
  g4 = 0;
}

cout << "Grade:  " << (g1+g2+g3 + g4)/3 << endl;
		if ((((g1+g2+g3 + g4)/3) >= 73))


Or keep totalgrade variable somewhere in there.
If you want it to be extensible, you could use a modifier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int gradeModifier = 0;

//....
//try to calculate average only once.
int averageGrade = (g1+g2+g3 + g4)/3 + gradeModifier;

//...
cout << "Grade:  " << averageGrade<< endl;
		if (((averageGrade >= 73))
		{cout << "Successful" << endl;}
		else
		{cout << "Unsuccessful" << endl;}



By doing it like this you can use different scenarios to modify the final grade.
Topic archived. No new replies allowed.