Using functions to generate a specific output

1. How do I fix the following code to print the mark per subject with it's corresponding symbol and code on separate lines?

2. How do i fix the code to print distinction details per subject?

3. How do I create a function to display the output data?

4. How can I neaten / shorten the 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
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
184
185
186
187
188
189
190
191
192
193
194
#include
using namespace std;

void studentDetails(string & nameP, string & surnameP, string & schoolNameP)
{
cout << "Enter name: ";
cin >> nameP;
cout << "Enter surname: ";
cin >> surnameP;
cout << "Enter the name of the school: ";
cin >> schoolNameP;
}

void getMarks(float & engP, float & mathP, float & life_orientP, float & histP, float & comp_litP, float & artP)
{
cout << "Enter your mark for English: ";
cin >> engP;
if (engP < 0 || engP > 100)
do
{
cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
cin >> engP;
} while (engP < 0 || engP > 100);
cout << "Enter your mark for Mathematics: ";
cin >> mathP;
if (mathP < 0 || mathP > 100)
do
{
cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
cin >> mathP;
} while (mathP < 0 || mathP > 100);
cout << "Enter your mark for Life Orientation: ";
cin >> life_orientP;
if (life_orientP < 0 || life_orientP > 100)
do
{
cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
cin >> life_orientP;
} while (life_orientP < 0 || life_orientP > 100);
cout << "Enter your mark for History: ";
cin >> histP;
if (histP < 0 || histP > 100)
do
{
cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
cin >> histP;
} while (histP < 0 || histP > 100);
cout << "Enter your mark for Computer Literacy: ";
cin >> comp_litP;
if (comp_litP < 0 || comp_litP > 100)
do
{
cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
cin >> comp_litP;
} while (comp_litP < 0 || comp_litP > 100);
cout << "Enter your mark for Art: ";
cin >> artP;
if (artP < 0 || artP > 100)
do
{
cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
cin >> artP;
} while (artP < 0 || artP > 100);
}

void awardDistinction(float & engP, float & mathP, float & life_orientP, float & histP, float & comp_litP, float & artP)
{
if ((engP + mathP + life_orientP + histP + comp_litP + artP) / 6 >= 75)
{
cout << "Passed with distinction" << endl;
}
else if (engP >= 75)
{
cout << "Subject distinction = English" << endl;
}
else if (mathP >= 75)
{
cout << "Subject distinction = Mathematics" << endl;
}
else if (life_orientP >= 75)
{
cout << "Subject distinction = Life Orientation" << endl;
}
else if (histP >= 75)
{
cout << "Subject distinction = History" << endl;
}
else if (comp_litP >= 75)
{
cout << "Subject distinction = Computer Literacy" << endl;
}
else if (artP >= 75)
{
cout << "Subject distinction = Art" << endl;
}

return;
}

void codeSymbol(float & engP, float & mathP, float & life_orientP, float & histP, float & comp_litP, float & artP, float & averageP, string & symbolP, int & codeP)
{
averageP = (engP + mathP + life_orientP + histP + comp_litP + artP) / 6;
if (engP >= 0 && engP <= 29 || mathP >= 0 && mathP <= 29 || life_orientP >= 0 && life_orientP <= 29 ||
histP >= 0 && histP <= 29 || comp_litP >= 0 && comp_litP <= 29 || artP >= 0 && artP <= 29 || averageP >= 0 && averageP <= 29)
{
symbolP = "FF";
codeP = 1;
cout << engP << '\t' << symbolP << '\t' << codeP << endl;
}
else if (engP >= 30 && engP <= 39 || mathP >= 30 && mathP <= 39 || life_orientP >= 30 && life_orientP <= 39 ||
histP >= 30 && histP <= 39 || comp_litP >= 30 && comp_litP <= 39 || artP >= 30 && artP <= 39 || averageP >= 30 && averageP <= 39)
{
symbolP = "F";
codeP = 2;
}
else if (engP >= 40 && engP <= 49 || mathP >= 40 && mathP <= 49 || life_orientP >= 40 && life_orientP <= 49 ||
histP >= 40 && histP <= 49 || comp_litP >= 40 && comp_litP <= 49 || artP >= 40 && artP <= 49 || averageP >= 40 && averageP <= 49)
{
symbolP = "E";
codeP = 3;
}
else if (engP >= 50 && engP <= 59 || mathP >= 50 && mathP <= 59 || life_orientP >= 50 && life_orientP <= 59 ||
histP >= 50 && histP <= 59 || comp_litP >= 50 && comp_litP <= 59 || artP >= 50 && artP <= 59 || averageP >= 50 && averageP <= 59)
{
symbolP = "D";
codeP = 4;
}
else if (engP >= 60 && engP <= 69 || mathP >= 60 && mathP <= 69 || life_orientP >= 60 && life_orientP <= 69 ||
histP >= 60 && histP <= 69 || comp_litP >= 60 && comp_litP <= 69 || artP >= 60 && artP <= 69 || averageP >= 60 && averageP <= 69)
{
symbolP = "C";
codeP = 5;
}
else if (engP >= 70 && engP <= 79 || mathP >= 70 && mathP <= 79 || life_orientP >= 70 && life_orientP <= 79 ||
histP >= 70 && histP <= 79 || comp_litP >= 70 && comp_litP <= 79 || artP >= 70 && artP <= 79 || averageP >= 70 && averageP <= 79)
{
symbolP = "B";
codeP = 6;
}
else if (engP >= 80 && engP <= 100 || mathP >= 80 && mathP <= 100 || life_orientP >= 80 && life_orientP <= 100 ||
histP >= 80 && histP <= 100 || comp_litP >= 80 && comp_litP <= 100 || artP >= 80 && artP <= 100 || averageP >= 80 && averageP <= 100)
{
symbolP = "A";
codeP = 7;
}

return;
}

int main( )
{
string name, surname, schoolName, outcome;
float eng, math, life_orient, hist, comp_lit, art, average;
float maximum = 1;
float minimum = 0;
int code;
string symbol;
string english = "English";
string mathematics = "Mathematics";
string life_orientation = "Life Orientation";
string history = "History";
string computer_literacy = "Computer Literacy";
string art1 = "Art";

studentDetails(name, surname, schoolName);

getMarks(eng, math, life_orient, hist, comp_lit, art);

average = calcAverageYearMark(eng, math, life_orient, hist, comp_lit, art);

minMax(eng, math, life_orient, hist, comp_lit, art, minimum, maximum);

passOrFail(eng, math, life_orient, hist, comp_lit, art, outcome);

awardDistinction(eng, math, life_orient, hist, comp_lit, art);

codeSymbol(eng, math, life_orient, hist, comp_lit, art, symbol, code);

cout << "***************************************************************************************************" << endl;
cout << "**************************************STUDENT ACADEMIC REPORT**************************************" << endl;
cout << "This program inputs the learner marks of matric level subjects and prints the students final report" << endl;
cout << "Name: " << name << " " << surname << "               School: " << schoolName << endl;
cout << "Subject\tMark\tSymbol\tCode" << endl;
cout << english << '\t' << eng << '\t' << symbol << '\t' << code << endl;
cout << mathematics << '\t' << math << '\t' << symbol << '\t' << code << endl;
cout << "Average year mark: " << average << " with Symbol " << symbol << " and Code " << code << endl;
cout << "Outcome: " << outcome << endl;
cout << "The highest mark was " << maximum << "%" << endl;
cout << "The lowest mark was " << minimum << "%" << endl;
cout << "***************************************************************************************************" << endl;


return 0;
}


Last edited on
Please edit for readability.
https://www.cplusplus.com/articles/jEywvCM9/
I apologise, brand new to this
There is still a lot of work to do on this program. But here is something to get you going...

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
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <string>

using namespace std;

void studentDetails(string& nameP, string& surnameP, string& schoolNameP)
{
	cout << "Enter name: ";
	cin >> nameP;
	cout << "Enter surname: ";
	cin >> surnameP;
	cout << "Enter the name of the school: ";
	cin >> schoolNameP;
}

void getMarks(float& engP, float& mathP, float& life_orientP, float& histP, float& comp_litP, float& artP)
{
	cout << "Enter your mark for English: ";
	cin >> engP;
	if (engP < 0 || engP > 100) {
		do
		{
			cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
			cin >> engP;
		} while (engP < 0 || engP > 100);
	}
	cout << "Enter your mark for Mathematics: ";
	cin >> mathP;
	if (mathP < 0 || mathP > 100) {
		do
		{
			cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
			cin >> mathP;
		} while (mathP < 0 || mathP > 100);
	}
	cout << "Enter your mark for Life Orientation: ";
	cin >> life_orientP;
	if (life_orientP < 0 || life_orientP > 100) {
		do
		{
			cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
			cin >> life_orientP;
		} while (life_orientP < 0 || life_orientP > 100);
	}
	cout << "Enter your mark for History: ";
	cin >> histP;
	if (histP < 0 || histP > 100) {
		do
		{
			cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
			cin >> histP;
		} while (histP < 0 || histP > 100);
	}
	cout << "Enter your mark for Computer Literacy: ";
	cin >> comp_litP;
	if (comp_litP < 0 || comp_litP > 100) {
		do
		{
			cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
			cin >> comp_litP;
		} while (comp_litP < 0 || comp_litP > 100);
	}
	cout << "Enter your mark for Art: ";
	cin >> artP;
	if (artP < 0 || artP > 100) {
		do
		{
			cout << "Value cannot be less than 0 or greater than 100. Enter again: ";
			cin >> artP;
		} while (artP < 0 || artP > 100);
	}
}

void awardDistinction(float& engP, float& mathP, float& life_orientP, float& histP, float& comp_litP, float& artP)
{
	if ((engP + mathP + life_orientP + histP + comp_litP + artP) / 6 >= 75)
	{
		cout << "Passed with distinction" << endl;
	}
	else {
		if (engP >= 75)
		{
			cout << "Subject distinction = English" << endl;
		}
		if (mathP >= 75)
		{
			cout << "Subject distinction = Mathematics" << endl;
		}
		if (life_orientP >= 75)
		{
			cout << "Subject distinction = Life Orientation" << endl;
		}
		if (histP >= 75)
		{
			cout << "Subject distinction = History" << endl;
		}
		if (comp_litP >= 75)
		{
			cout << "Subject distinction = Computer Literacy" << endl;
		}
		if (artP >= 75)
		{
			cout << "Subject distinction = Art" << endl;
		}
	}
}

void calcAverageYearMark(float& engP, float& mathP, float& life_orientP, float& histP, float& comp_litP, float& artP, float& averageP) {
	averageP = (engP + mathP + life_orientP + histP + comp_litP + artP) / 6;
}

void codeSymbol(float subject, string & symbol, int& code) {
	if (subject <= 29) {
		symbol = "FF";
		code = 1;
	}
	else if (subject >= 30 && subject <= 39) {
		symbol = "F";
		code = 2;
	}
	else if (subject >= 40 && subject <= 49) {
		symbol = "E";
		code = 3;
	}
	else if (subject >= 50 && subject <= 59) {
		symbol = "D";
		code = 4;
	}
	else if (subject >= 60 && subject <= 69) {
		symbol = "C";
		code = 5;
	}
	else if (subject >= 70 && subject <= 79) {
		symbol = "B";
		code = 6;
	}
	else if (subject >= 80 && subject <= 100) {
		symbol = "A";
		code = 7;
	}
}

void display(float eng, float math, float life_orient, float hist, 
			float comp_lit, float art, float average, string& symbol, 
			int& code, string outcome, float maximum, float minimum, string name, string surname, string schoolName) {
	cout << "***************************************************************************************************" << endl;
	cout << "**************************************STUDENT ACADEMIC REPORT**************************************" << endl;
	cout << "This program inputs the learner marks of matric level subjects and prints the students final report" << endl;
	cout << "Name: " << name << " " << surname << " School: " << schoolName << endl;
	cout << "Subject\tMark\tSymbol\tCode" << endl;
	codeSymbol(eng, symbol, code);
	cout << "English" << '\t' << eng << '\t' << symbol << '\t' << code << endl;
	codeSymbol(math, symbol, code);
	cout << "Mathematics" << '\t' << math << '\t' << symbol << '\t' << code << endl;
	codeSymbol(average, symbol, code);
	cout << "Average year mark: " << average << " with Symbol " << symbol << " and Code " << code << endl;
	cout << "Outcome: " << outcome << endl;
	cout << "The highest mark was " << maximum << "%" << endl;
	cout << "The lowest mark was " << minimum << "%" << endl;
	cout << "***************************************************************************************************" << endl;

}


int main()
{
	string name, surname, schoolName, outcome;
	float eng, math, life_orient, hist, comp_lit, art, average;
	float maximum = 1;
	float minimum = 0;
	int code;
	string symbol;


	studentDetails(name, surname, schoolName);

	getMarks(eng, math, life_orient, hist, comp_lit, art);

	calcAverageYearMark(eng, math, life_orient, hist, comp_lit, art, average);

	//minMax(eng, math, life_orient, hist, comp_lit, art, minimum, maximum);

	//passOrFail(eng, math, life_orient, hist, comp_lit, art, outcome);

	awardDistinction(eng, math, life_orient, hist, comp_lit, art);


	display(eng, math, life_orient, hist, comp_lit, art, average, symbol, code, outcome, maximum, minimum, name, surname, schoolName);


	return 0;
}
Thank you, this helps a lot.
How do I get the subject, mark, symbol and code aligned correctly? They should be in columns.
a common way is to use tabs, the '\t' character:
cout << word << '\t' << word2 << …
you can also work with the <iomanip> tools, esp for numbers.
Last edited on
How do you use the std::setw with more than one variable in a cout statement?
How do you use the std::setw with more than one variable in a cout statement?

Use setw() multiple times.

 
cout << setw(5) << a << setw(3) << b << endl;
Thank you! You guys are wonderful people!
Topic archived. No new replies allowed.