Report Card

Write a program that prompts the learner to key in their marks for each subject. The program should include the following functions:
a.A function studentDetails, that prompts the learner to key in their personal details
name,surname, and schoolName.
b.A function getMarks,that prompts the learner to key in a mark for each of the six subjects, andvalidate the marks. Do not accept marks lower than 0 or higher than 100
c.A function calcAverageYearMark, to calculate and display the average of the 6 Subjects. Thisfunction should be called just once by main, and should be passed the 6 Subject marks.
d.A function minMax, to find and return the lowest and the highest of the 6 subject marks
passed to it as the subject with the lowest mark;
e.A function passOrFail, to determine whether the student has passed or failed grade 12.
f.A function awardDistinctionto determine which of the subjects have receiveddistinctions. A subject receives a distinction if the mark is 75% and above. Also a studenthas passed with distinction if the average mark is 75% and above.
g.A function codeSymbol,to convert each mark to a symbol (A, B, C, D, E, F) and a code(7,6,5,4,3,2,1). The symbol and code should be printed next to the mark in the studentreport. The same should be calculated and displayed for the average mark.
h.A function to Display the student report.

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
#include <iostream>
#include <string>
using namespace std;

double marks[6];


int minMax(double marks[6])
{
  double max;
  double min;
  double minIndex;
  double maxIndex;
  for (int i = 0; i < 6; ++i) {
    max = 0;
    if (marks[i] > max) {
      marks[i] = max;
      maxIndex = i;
    }
  }

  for (int i = 0; i < 6; ++i) {
    min = 0;
    if (marks[i] < min) {
      marks[i] = min;
      minIndex = i;
    }
  }
  cout << "Your lowest mark was " << minIndex << endl;
  cout << "Your highest mark is for " << maxIndex << endl;

}

int calcAverageYearMark(double eng, double math, double lo, double hist, double clit, double geo)
{
  double totMark;
  double avgMark;

  totMark = marks[0] + marks[1] + marks[2] + marks[3] + marks[4] + marks[5];
  avgMark = totMark / 6;

  cout << "Your averge mark is " << avgMark << endl;
}

int getMarks(double eng, double math, double lo, double hist, double clit, double geo)
{
  cout << "Key in your mark for English: ";
  cin >> eng;
  cout << "Key in your mark for Mathematics: ";
  cin >> math;
  cout << "Key in your mark for Life Orientation: ";
  cin >> lo;
  cout << "Key in your mark for History: ";
  cin >> hist;
  cout << "Key in your mark for Computer Literacy: ";
  cin >> clit;
  cout << "Key in your mark for Art: ";
  cin >> geo;

  marks[0] = eng;
  marks[1] = math;
  marks[2] = lo;
  marks[3] = hist;
  marks[4] = clit;
  marks[5] = geo;
}


int studentDetails(string name,string school)
{
  cout << "Please key in your name: " << endl;
  cin >> name;
  std::getline(std::cin,name);
  cout << "Please key in the name of your school: " << endl;
  cin >> school;
  std::getline(std::cin,school);


}


void passOrFail()
{
}

void awardDistinction()
{
}


void codeSymbol()
{

}





void Display()
{
    cout << " ****************************************************************************************************************** " << endl;
    cout << "                                              STUDENT ACADEMIC RECORD                                               " << endl;
    cout << "  This program inputs the learner marks of matric level subjects and prints the student final report" << endl;
    cout << " ****************************************************************************************************************** " << endl;
    cout << " Name : " << name << "                                                          School : " << school << endl;
    cout << " Subject                      Mark                          Symbol                     Code" << endl;
    cout << " English"              <<     marks[0]           <<         symbol    <<               code << endl;
    cout << " Mathematics"          <<     marks[1]           <<         symbol    <<               code << endl;
    cout << " Life Orientation"     <<     marks[2]           <<         symbol    <<               code << endl;
    cout << " History"              <<     marks[3]           <<         symbol    <<               code << endl;
    cout << " Computer Literacy"    <<     marks[4]           <<         symbol    <<               code << endl;
    cout << " Geography"            <<     marks[5]           <<         symbol    <<               code << endl << endl;
    cout << " Average Year Mark: " << avgMark << "with Symbol " << symbol << " and code " << code "." << endl;
    cout << "Outcome: " << passorFail << endl << endl;
    cout << "The highest mark was " << maxIndex << " and the lowest mark was " << minIndex << "." << endl;
    cout << " ******************************************************************************************************************* " << endl;
}


int main()
{
  string name;
  string school;
  double eng;
  double math;
  double lo;
  double hist;
  double clit;
  double geo;

  studentDetails(name,school);
  getMarks(eng, math, lo, hist, clit, geo);
  calcAverageYearMark(eng, math, lo, hist, clit, geo);
  minMax(marks);
  return 0;
}


This is what I have so far. For the passOrFail , I will use a boolean right? and a Switch statement will help on the codeSymbol function?

I know the display won't display yet as I still haven't done the "code" and "symbols" part
Last edited on
When I tried to run your code on cpp.sh I got the following errors:
Don't try to write to complete code at once.
It's much easier to write and test the code in small chunks.
In function 'int minMax(double*)':
32:1: warning: no return statement in function returning non-void [-Wreturn-type]
 In function 'int calcAverageYearMark(double, double, double, double, double, double)':
43:1: warning: no return statement in function returning non-void [-Wreturn-type]
 In function 'int getMarks(double, double, double, double, double, double)':
66:1: warning: no return statement in function returning non-void [-Wreturn-type]
 In function 'int studentDetails(std::string, std::string)':
79:1: warning: no return statement in function returning non-void [-Wreturn-type]
 In function 'void Display()':
106:27: error: 'name' was not declared in this scope
106:108: error: 'school' was not declared in this scope
108:74: error: 'symbol' was not declared in this scope
108:101: error: 'code' was not declared in this scope
114:39: error: 'avgMark' was not declared in this scope
115:28: error: 'passorFail' was not declared in this scope
116:40: error: 'maxIndex' was not declared in this scope
116:83: error: 'minIndex' was not declared in this scope
 
For a starter, the minMax() function is not correct. Try working through it by hand using pen & paper and you'll see. Also, it's definition says that it should return a value but it doesn't. Either return a value or change the return type to void.

For calcAverageYearMark(), it passes 6 params for the subject marks, but within the function uses the global array marks?? Also again it says that the function should return an int value but it doesn't return any value.

Non-const global variables are a bad idea and should be avoided. I'd suggest removing global marks array and either pass an array to the functions (as per minMax() ) or pass the 6 marks as per calcAverageYearMark().

In studentDetails(), you ask for info about a student but as the params are passed by value and not by ref, the entered data is passed back to the calling function.
For the passOrFail , I will use a boolean right?
Yes. passOrFail() should return a bool

switch statement will help on the codeSymbol function?
No, use an if/then/else ladder:
1
2
3
4
5
if (grade < 60) {
    letter = 'F';
} else (if grade < 70) {
    letter = 'D';
} else ....

Don't code it like this:
1
2
3
4
5
if (grade > 0.0 && grade < 60.0) {
   letter = 'F';
} else if (grade >= 61.0 && grade < 70) {
   letter = 'D';
} ....

Do you see why? The latter has bugs. For example, a grade of 0.0 will not match any of the conditions, nor will 60.5. To avoid this sort of "falling through the cracks" problem, it's best to use the one-way comparisons of the first example.

Outside of your questions, I urge you to copy the descriptions of each function and paste them as a comment above each function in the code. For example:
1
2
3
4
// d.A function minMax, to find and return the lowest and the highest
// of the 6 subject marks
int minMax(double marks[6])
{

This helps remind you exactly what the function should do. I know it sounds petty, but trust me, it really helps to prevent easily avoidable mistakes in your code. For example, when I add the comment above, I immediately think "how will it return two values?" Looking at the code, it's also clear that you don't return the value at all (maxIndex and minIndex are local variables).

calcAverageYearMark() passes the individual marks but doesn't use them. Pass the marks array to the function instead.

Same comment for getMarks. Also, getMarks should probably have a loop so it can check the value for each mark within the loop.

Adding the comment to studentDetails, it's also immediately clear that your entering the name as one string instead of entering both name and surname. How will you return the details from this function?

I suggest you take a half-step back and figure out exactly what the parameters and returns are for each function. After all, you can't write correct code if you don't know what the inputs and outputs are.
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <string>
using namespace std;



string name,school;
double maximum, minimum, sum, average;

double marks1;
double marks2;
double marks3;
double marks4;
double marks5;
double marks6;


void studentDetails();
void getMarks();
double calcAverageYearMark();
void passOrFail();
double minMax(double& minimum, double& maximum);
void awardDistinction();



double minMax()
{
    if (marks1 > marks2 && marks1 > marks3 && marks1 > marks4 && marks1 > marks5 &&
        marks1 > marks6){
        cout << "The highest is" << marks1;
    } else if (marks2 > marks1 && marks2 > marks3 && marks2 > marks4 && marks2 > marks5 &&
               marks2 > marks6){
        cout << "The highest is " << marks2;
    } else if (marks3 > marks1 && marks3 > marks2 && marks3 > marks4 && marks3 > marks5 &&
               marks3 > marks6){
        cout << "The highest is " << marks3;
    } else if (marks4 > marks1 && marks4 > marks2 && marks4 > marks3 && marks4 > marks5 &&
               marks4 > marks6){
        cout << "The highest is " << marks4;
    } else if (marks5 > marks1 && marks5 > marks2 && marks5 > marks3 && marks5 > marks4 &&
               marks5 > marks6){
        cout << "The highest is" << marks5;
    } else if (marks6 > marks1 && marks6 > marks2 && marks6 > marks3 && marks6 > marks4 &&
               marks6 > marks5){
        cout << "The highest is " << marks6;
    }

    if (marks1 < marks2 && marks1 < marks3 && marks1 < marks4 && marks1 < marks5 &&
        marks1 < marks6) {
        cout << "The lowest is" << marks1;
    } else if (marks2 < marks1 && marks2 < marks3 && marks2 < marks4 && marks2 < marks5 &&
               marks2 < marks6) {
        cout << "The lowest is " << marks2;
    } else if (marks3 < marks1 && marks3 < marks2 && marks3 < marks4 && marks3 < marks5 &&
               marks3 < marks6) {
        cout << "The lowest is " << marks3;
    } else if (marks4 < marks1 && marks4 < marks2 && marks4 < marks3 && marks4 < marks5 &&
               marks4 < marks6) {
        cout << "The lowest is " << marks4;
    } else if (marks5 < marks1 && marks5 < marks2 && marks5 < marks3 && marks5 < marks4 &&
               marks5 < marks6) {
        cout << "The lowest is" << marks5;
    } else if (marks6 < marks1 && marks6 < marks2 && marks6 < marks3 && marks6 < marks4 &&
               marks6 < marks5) {
        cout << "The lowest is " << marks6;
    }
}


double calcAverageYearMark()
{
    sum = marks1 + marks2 + marks3 + marks4 + marks5 + marks6;
    average = sum / 6;
    return average;

}

void getMarks()
{
    cout << "Please enter your mark for English:" << endl;
    cin >> marks1;

    cout << "Please enter your mark for Mathematics:" << endl;
    cin >> marks2;

    cout << "Please enter your mark for Life Orientation:" << endl;
    cin >> marks3;

    cout << "Please enter your mark for History:" << endl;
    cin >> marks4;

    cout << "Please enter your mark for Computer Literacy:" << endl;
    cin >> marks5;

    cout << "Please enter your mark for Geography:" << endl;
    cin >> marks6;
}




void studentDetails(string name,string school)
{
  cout << "Please key in your name: " << endl;
  cin >> name;
  std::getline(std::cin,name);
  cout << "Please key in the name of your school: " << endl;
  cin >> school;
  std::getline(std::cin,school);


}


void passOrFail ()
 {
    if (marks1 >= 0, marks2 >= 50, marks3 >= 0, marks4 >= 50)
        if (marks1 >= 50, marks2 >= 50, marks3 >= 50, marks5 >= 50)
            if (marks1 >= 50, marks2 >= 50, marks3 >= 50, marks6 >= 50)
                if (marks1 >= 50, marks3 >= 50, marks4 >= 50, marks5 >= 50)
                    if (marks1 >= 50, marks3 >= 50, marks4 >= 50, marks6 >= 50)
                        if (marks1 >= 50, marks4 >= 50, marks5 >= 50, marks6 >= 50)

                        {
                            cout << "Congradulations !!! You passed your matric" << endl;

                        }
                        else
                        {
                            cout << "Sorry you did not make it, you did not pass English" << endl;
                        }

}

void awardDistinction()

{
    if (marks1 >= 75)
    {
        cout << "Pass with  Distinction" << endl;
    }
    else
        if (marks2 >= 75)
        {
            cout << "Pass with  Distinction" << endl;
        }
        else
            if (marks3 >= 75)
            {
                cout << "Pass with  Distinction" << endl;
            }
            else
                if (marks4 >= 75)
                {
                    cout << "Pass with  Distinction" << endl;
                }
                else
                    if (marks5 >= 75)
                    {
                        cout << "Pass with  Distinction" << endl;
                    }
                    else
                        if (marks6 >= 75)
                        {
                            cout << "Pass with  Distinction" << endl;
                        }
}



void codeSymbol()
{

}





void Display()
{
    cout << " ****************************************************************************************************************** " << endl;
    cout << "                                              STUDENT ACADEMIC RECORD                                               " << endl;
    cout << "  This program inputs the learner marks of matric level subjects and prints the student final report" << endl;
    cout << " ****************************************************************************************************************** " << endl;
    cout << " Name : " << name << "                                                          School : " << school << endl;
    cout << " Subject                      Mark                          Symbol                     Code" << endl;
    cout << " English"              <<     marks1                                     << endl;
    cout << " Mathematics"          <<     marks2                                     << endl;
    cout << " Life Orientation"     <<     marks3                                     << endl;
    cout << " History"              <<     marks4                                     << endl;
    cout << " Computer Literacy"    <<     marks5                                     << endl;
    cout << " Geography"            <<     marks6                                     << endl << endl;
    cout << " Average Year Mark: " << average << "with Symbol " /*symbol*/ << " and code " << /*code*/ "." << endl;
    cout << "Outcome: " << passOrFail << endl << endl;

    cout << " ******************************************************************************************************************* " << endl;
}


int main(){
    studentDetails();
    getMarks();
    cout << "the average is " << calcAverageYearMark() << endl;


    return 0;
}


does this look a little better so far?
Well it doesn't compile for a start!

Have you noted and understood my earlier comment re not using non-const global variables and other comments above?

The passOrFail() function is not correct. L118-123 don't do what you are trying to do.

As a first refactor, consider which compiles and runs.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <type_traits>
#include <charconv>

constexpr size_t NOMARKS { 6 };
constexpr int MinMark { 0 };
constexpr int MaxMark { 100 };
constexpr int Dist { 75 };

using Marks = int[NOMARKS];
using std::cout;
using std::cin;
using std::left;
using std::setw;

struct MinMax {
	int minMark {};
	int maxMark {};
};

template<typename T = int>
typename std::enable_if_t< std::is_arithmetic_v<T>, T>
getInp(const std::string& prm, T mn, T mx) {
	T num {};

	for (std::string inp;;) {
		std::cin.clear();
		std::cout << prm;

		if (std::getline(std::cin, inp) && !inp.empty()) {
			const auto first { std::find_if(inp.begin(), inp.end(), [](unsigned ch) {return !std::isspace(ch); }) };
			const auto end { std::find_if(inp.rbegin(), inp.rend(), [](unsigned ch) {return !std::isspace(ch); }).base() };
			const auto res { std::from_chars(std::to_address(first), std::to_address(end), num) };

			if (res.ec == std::errc {} && res.ptr == std::to_address(end))
				if (num >= mn && num <= mx)
					return num;
				else {
					std::cout << "Value not in range " << mn << " - " << mx << '\n';
					continue;
				}
		}

		std::cout << "Invalid input. Not a(n) " << typeid(T).name() << '\n';
	}
}

MinMax minMax(const Marks marks) {
	int mn { marks[0] }, mx { marks[0] };

	for (size_t i { 1 }; i < NOMARKS; ++i) {
		if (marks[i] < mn)
			mn = marks[i];

		if (marks[i] > mx)
			mx = marks[i];
	}

	return { mn, mx };
}

double calcAverageYearMark(const Marks marks) {
	double sum {};

	for (size_t i {}; i < NOMARKS; ++i)
		sum += marks[i];

	return sum / NOMARKS;
}

void getMarks(Marks marks) {
	marks[0] = getInp("Please enter your mark for English: ", MinMark, MaxMark);
	marks[1] = getInp("Please enter your mark for Mathematics: ", MinMark, MaxMark);
	marks[2] = getInp("Please enter your mark for Life Orientation: ", MinMark, MaxMark);
	marks[3] = getInp("Please enter your mark for History: ", MinMark, MaxMark);
	marks[4] = getInp("Please enter your mark for Computer Literacy: ", MinMark, MaxMark);
	marks[5] = getInp("Please enter your mark for Geography: ", MinMark, MaxMark);
}

void studentDetails(std::string& name, std::string& school) {
	cout << "Please key in your name: ";
	std::getline(std::cin, name);

	cout << "Please key in the name of your school: ";
	std::getline(std::cin, school);
}

void passOrFail() {
}

bool awardDistinction(int mark) {
	return mark >= Dist;
}

void codeSymbol()
{

}

void display(const Marks marks, const std::string& name, const std::string& school) {
	cout << " ******************************************************************************************************************\n";
	cout << "                                              STUDENT ACADEMIC RECORD                                               \n";
	//cout << "  This program inputs the learner marks of matric level subjects and prints the student final report" << endl;
	cout << " ******************************************************************************************************************\n";
	cout << " Name : " << name << "                                                          School : " << school << '\n';
	cout << " Subject                      Mark                          Symbol                     Code\n";
	cout << left << setw(30) << " English" << marks[0] << '\n';
	cout << left << setw(30) << " Mathematics" << marks[1] << '\n';
	cout << left << setw(30) << " Life Orientation" << marks[2] << '\n';
	cout << left << setw(30) << " History" << marks[3] << '\n';
	cout << left << setw(30) << " Computer Literacy" << marks[4] << '\n';
	cout << left << setw(30) << " Geography" << marks[5] << '\n';
	cout << " The average mark is " << calcAverageYearMark(marks) << '\n';

	const auto mnx { minMax(marks) };

	cout << " The minimum mark is " << mnx.minMark << '\n';
	cout << " The maxmimum mark is " << mnx.maxMark << '\n';
	//cout << " Average Year Mark: " << average << "with Symbol " /*symbol*/ << " and code " << /*code*/ "." << endl;
	//cout << "Outcome: " << passOrFail << endl << endl;

	cout << " *******************************************************************************************************************\n";
}

int main() {
	std::string name, school;
	Marks marks;

	studentDetails(name, school);
	getMarks(marks);
	display(marks, name, school);
}


What are the specs for passOrFail() and codeSymbol()?

Last edited on
does this look a little better so far?
That looks worse. You've lost your array of scores.
As a possible second refactor, consider:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <type_traits>
#include <charconv>
#include <iterator>

constexpr int MinMark { 0 };
constexpr int MaxMark { 100 };
constexpr int Dist { 75 };
constexpr const char* subjects[] { "English", "Mathematics", "Life Orientation", "History", "Computer Literacy", "Geography" };
constexpr size_t NOMARKS { std::size(subjects)};

using Marks = int[NOMARKS];
using std::cout;
using std::cin;
using std::left;
using std::setw;
using std::setfill;
using namespace std::string_literals;

struct MinMax {
	int minMark {};
	int maxMark {};
};

template<typename T = int>
typename std::enable_if_t< std::is_arithmetic_v<T>, T>
getInp(const std::string& prm, T mn, T mx) {
	T num {};

	for (std::string inp;;) {
		std::cin.clear();
		std::cout << prm;

		if (std::getline(std::cin, inp) && !inp.empty()) {
			const auto first { std::find_if(inp.begin(), inp.end(), [](unsigned ch) {return !std::isspace(ch); }) };
			const auto end { std::find_if(inp.rbegin(), inp.rend(), [](unsigned ch) {return !std::isspace(ch); }).base() };
			const auto res { std::from_chars(std::to_address(first), std::to_address(end), num) };

			if (res.ec == std::errc {} && res.ptr == std::to_address(end))
				if (num >= mn && num <= mx)
					return num;
				else {
					std::cout << "Value not in range " << mn << " - " << mx << '\n';
					continue;
				}
		}

		std::cout << "Invalid input. Not a(n) " << typeid(T).name() << '\n';
	}
}

const char* yesNo(bool yn) {
	static constexpr const char* yesno[2] {"no", "yes"};

	return yesno[yn];
}

MinMax minMax(const Marks marks) {
	int mn { marks[0] }, mx { marks[0] };

	for (size_t i { 1 }; i < NOMARKS; ++i) {
		if (marks[i] < mn)
			mn = marks[i];

		if (marks[i] > mx)
			mx = marks[i];
	}

	return { mn, mx };
}

double calcAverageYearMark(const Marks marks) {
	double sum {};

	for (size_t i {}; i < NOMARKS; ++i)
		sum += marks[i];

	return sum / NOMARKS;
}

void getMarks(Marks marks) {
	for (size_t m {}; m < NOMARKS; ++m)
		marks[m] = getInp("Please enter your mark for "s + subjects[m] + ": "s, MinMark, MaxMark);
}

void studentDetails(std::string& name, std::string& school) {
	cout << "Please key in your name: ";
	std::getline(std::cin, name);

	cout << "Please key in the name of your school: ";
	std::getline(std::cin, school);
}

void passOrFail() {
}

bool awardDistinction(double mark) {
	return mark >= Dist;
}

void codeSymbol()
{

}

#define LINE cout << setw(110) << setfill('*') << "*" << setfill(' ') << '\n'

void display(const Marks marks, const std::string& name, const std::string& school) {
	cout << '\n';
	LINE;
	cout << std::right << setw(66) << "STUDENT ACADEMIC RECORD" << '\n';
	LINE;
	cout << "Name : " << name << std::right << setw(66) << "School : " << school << "\n\n";
	cout << left << setw(30) << "Subject" << setw(8) << "Mark" << setw(8) << "Dist" << setw(8) << "Symbol" << setw(8) << "Code" << '\n';

	for (size_t m {}; m < NOMARKS; ++m)
		cout << left << setw(30) << subjects[m] << setw(8) << marks[m] << setw(8) << yesNo(awardDistinction(marks[m])) << '\n';

	const auto avg { calcAverageYearMark(marks) };
	const auto mnx { minMax(marks) };

	cout << "\nThe average mark is " << avg << '\n';
	cout << "The minimum mark is " << mnx.minMark << '\n';
	cout << "The maxmimum mark is " << mnx.maxMark << '\n';
	cout << "Overall distinction: " << yesNo(awardDistinction(avg)) << '\n';
	//cout << " Average Year Mark: " << average << "with Symbol " /*symbol*/ << " and code " << /*code*/ "." << endl;
	//cout << "Outcome: " << passOrFail << endl << endl;
	LINE;
}

int main() {
	std::string name, school;
	Marks marks;

	studentDetails(name, school);
	getMarks(marks);
	display(marks, name, school);
}



Please key in your name: ewewe
Please key in the name of your school: fdfdfd
Please enter your mark for English: 67
Please enter your mark for Mathematics: 98
Please enter your mark for Life Orientation: 99
Please enter your mark for History: 78
Please enter your mark for Computer Literacy: 67
Please enter your mark for Geography: 99

**************************************************************************************************************
                                           STUDENT ACADEMIC RECORD
**************************************************************************************************************
Name : ewewe                                                         School : fdfdfd

Subject                       Mark    Dist    Symbol  Code
English                       67      no
Mathematics                   98      yes
Life Orientation              99      yes
History                       78      yes
Computer Literacy             67      no
Geography                     99      yes

The average mark is 84.6667
The minimum mark is 67
The maxmimum mark is 99
Overall distinction: yes
**************************************************************************************************************

Last edited on
In addition to what I said earlier, check with the professor on this requirement:
c.A function calcAverageYearMark, to calculate and display the average of the 6 Subjects. This function should be called just once by main, and should be passed the 6 Subject marks.
It's very awkward to to have calcAverageYearMark() display the average. Since Display() is supposed to print the student report, it means that calcAverage prints that one part of the report. And since it must be called from main, the average must appear before or after the rest of the information.

How to marks (0-100) map to symbols A-F and codes 1-7? In the USA, There is typically no "E" grade, just A (90-100), B(80-90), C(70-80), D(60-70) and F(0-60).
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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void studentDetails(string&, string&, string&);
void getMarks(double[], string[], int);
double calcAverageYearMark(double[], int);
void minmaxIndex(double[], int, int&, int&);

bool passOrFail(int);
bool distinction(int);

void displayReport(string, string, string, double[], string[], int);

int main()
{
    const int NO_SUBJECTS{6};
    double marks[NO_SUBJECTS]{0};
    string subject[NO_SUBJECTS]
    {
        "English","Mathematics","Life Orientation","History",
        "Computer Literacy","Art"
    };
    
    
    string name, surname, school;
    studentDetails(name, surname, school);
    getMarks(marks, subject, NO_SUBJECTS);
    displayReport(name, surname, school, marks, subject, NO_SUBJECTS);
    
    return 0;
}

void studentDetails(string& nm, string& sn, string& sc)
{
    cout << "             Please key in your name: ";
    std::getline(std::cin, nm);
    
    cout << "          Please key in your surname: ";
    std::getline(std::cin, sn);
    
    cout << "Please key in the name of your school: ";
    std::getline(std::cin, sc);
}


void getMarks(double mks[], string subj[], int no_subj)
{
    for(int i = 0; i < no_subj; i++)
    {
        cout << "Key in your mark for " << subj[i] << ": ";
        cin >> mks[i];
    }
}


double calcAverageYearMark(double mks[], int no_subj)
{
    double totMark{0};
    
    for(int i = 0; i < no_subj; i++)
    {
        totMark += mks[i];
    }
    
    return totMark/no_subj;
}


void minmaxIndex(double mks[], int no_subj, int& mini, int& maxi)
{
    double min{1000};
    double max{-1};
    
    for (int i = 0; i < no_subj; ++i)
    {
        if (mks[i] < min)
        {
            min = mks[i];
            mini = i;
        }
        
        if (mks[i] > max)
        {
            max = mks[i];
            maxi = i;
        }
    }
}


int minIndex(double mks[], int no_subj)
{
    double max{1000};
    double index{0};
    
    for (int i = 0; i < no_subj; ++i)
    {
        if (mks[i] < max)
        {
            max = mks[i];
            index = i;
        }
    }
    return index;
}


bool passOrFail(int mks)
{
    if(mks >= 50)
        return true;
    else
        return false;
}

bool distinction(int mks)
{
    if(mks >= 75)
        return true;
    else
        return false;
}


void displayReport(string nm, string sn, string sc, double mks[], string subj[], int no_subj)
{
    cout
    << "REPORT FOR:\n"
    << "  NAME: " << nm << " SURNAME: " << sn
    << " SCHOOL: " << sc << "\n\n";
    
    for(int i = 0; i < no_subj; i++)
    {
        cout
        << setw(3) << i << ' ' << setw(20)  << subj[i] << '\t' << mks[i] << '\t';
        if( passOrFail(mks[i]) )
            cout << "Pass";
        else
            cout << "Fail";
        
        if( distinction( mks[i]) )
            cout << " Distinction";
        
        cout << '\n';
    }
    
    double average_mark = calcAverageYearMark(mks, no_subj);
    cout << "Average marks: " << average_mark;
    
    if( distinction( average_mark ) )
        cout << " Distinction";
    
    cout << '\n';
    
    int min_index{0};
    int max_index{0};
    minmaxIndex(mks, no_subj, min_index, max_index);
    cout << "Min marks: " << mks[min_index] << '\n';
    cout << "Max marks: " << mks[max_index] << '\n';
}


Please key in your name: Joan
          Please key in your surname: Bloggs-Smith
Please key in the name of your school: High Street Upper
Key in your mark for English: 34
Key in your mark for Mathematics: 87
Key in your mark for Life Orientation: 78
Key in your mark for History: 56
Key in your mark for Computer Literacy: 90
Key in your mark for Art: 67
REPORT FOR:
  NAME: Joan SURNAME: Bloggs-Smith SCHOOL: High Street Upper

  0              English	34	Fail
  1          Mathematics	87	Pass Distinction
  2     Life Orientation	78	Pass Distinction
  3              History	56	Pass
  4    Computer Literacy	90	Pass Distinction
  5                  Art	67	Pass
Average marks: 68.6667
Min marks: 34
Max marks: 90
Program ended with exit code: 0


Last edited on
This is more what I had in mind as an improvement. My minimum changes are:
* make up something that holds subject name
* make up a constant that replaces the 6 subjects, we don't like hard coded numbers
* fix minMax and show how it's now used
* remove distinct score variables and just use the existing array of marks
* do something about failOrPass()
* revise Display, dropping fields I don't understand, but you can add them

Add the new 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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

constexpr int nCourses = 6;
enum CourseType {English, Mathematics, LifeOrientation, History, ComputerLiteracy, Geography};
struct Course {
    CourseType type;
    const char* name;
};

constexpr Course courseDetails[nCourses] = {
    {English, "English"},
    {Mathematics, "Mathematics"},
    {LifeOrientation, "Life Orientation"},
    {History, "History"},
    {ComputerLiteracy, "Computer Literacy"},
    {Geography, "Geography"}
};
double marks[nCourses];

void minMax(double& minValue, double& maxValue, int& minIndex, int& maxIndex)
{
  minValue = marks[0];
  maxValue = 0.0;
  minIndex = 0;
  maxIndex = -1;

  for (int i = 0; i < nCourses; ++i) {
    if (marks[i] < minValue) {
      minValue = marks[i];
      minIndex = i;
    }
  }
  for (int i = 0; i < nCourses; ++i) {
    if (marks[i] > maxValue) {
      maxValue = marks[i];
      maxIndex = i;
    }
  }
}

double calcAverageYearMark()
{
  double totMark = 0.0;
  double avgMark = 0.0;

  for (int i = 0; i < nCourses; ++i) {
    totMark += marks[i];
  }
  avgMark = totMark / nCourses;

  return avgMark;
}

void getMarks()
{
  cout << "Key in your mark for English: ";
  cin >> marks[English];
  cout << "Key in your mark for Mathematics: ";
  cin >> marks[Mathematics];
  cout << "Key in your mark for Life Orientation: ";
  cin >> marks[LifeOrientation];
  cout << "Key in your mark for History: ";
  cin >> marks[History];
  cout << "Key in your mark for Computer Literacy: ";
  cin >> marks[ComputerLiteracy];
  cout << "Key in your mark for Geography: "; // or is Art?
  cin >> marks[Geography];
  cout << endl;
}

void studentDetails(string& name, string& school)
{
  cout << "Please key in your name: " << endl;
  std::getline(std::cin, name);
  cout << "Please key in the name of your school: " << endl;
  std::getline(std::cin, school);
  cout << endl;
}

bool passOrFail()
{
    // calculate from marks and some pass mark
    return false;  // until then, fail
}

std::string passOrFailAsString()
{
  if (passOrFail())
    return "pass";
  return "fail";
}

void awardDistinction()
{
}

void codeSymbol()
{
}

void Display(std::string name, std::string school)
{
  double minValue, maxValue;
  int minIndex, maxIndex;
  minMax(minValue, maxValue, minIndex, maxIndex);

  cout << " ****************************************************************************************************************** " << endl;
  cout << "                                              STUDENT ACADEMIC RECORD                                               " << endl;
  cout << "  This program inputs the learner marks of matric level subjects and prints the student final report" << endl;
  cout << " ****************************************************************************************************************** " << endl;
  cout << " Name : " << name << "\tSchool : " << school << endl;
  cout << " " << setw(20) << left << "Subject" << "Marks" << endl;
  for (int i = 0; i < nCourses; ++i) {
      cout << " " << setw(20) << left << courseDetails[i].name << marks[i] << '\n'; 
  }
  cout << " The highest mark was " << maxValue << " (" << courseDetails[maxIndex].name << ")\n";
  cout << " The lowest mark was " << minValue << " (" << courseDetails[minIndex].name  << ")\n";
  cout << " Outcome: " << passOrFailAsString() << endl;
  cout << " ******************************************************************************************************************* " << endl;

#ifdef UNHIDE_OLD_CODE
  cout << " ****************************************************************************************************************** " << endl;
  cout << "                                              STUDENT ACADEMIC RECORD                                               " << endl;
  cout << "  This program inputs the learner marks of matric level subjects and prints the student final report" << endl;
  cout << " ****************************************************************************************************************** " << endl;
  cout << " Name : " << name << "                                                          School : " << school << endl;
  cout << " Subject                      Mark                          Symbol                     Code" << endl;
  cout << " English"              <<     marks[0]           <<         symbol    <<               code << endl;
  cout << " Mathematics"          <<     marks[1]           <<         symbol    <<               code << endl;
  cout << " Life Orientation"     <<     marks[2]           <<         symbol    <<               code << endl;
  cout << " History"              <<     marks[3]           <<         symbol    <<               code << endl;
  cout << " Computer Literacy"    <<     marks[4]           <<         symbol    <<               code << endl;
  cout << " Geography"            <<     marks[5]           <<         symbol    <<               code << endl << endl;
  cout << " Average Year Mark: " << avgMark << "with Symbol " << symbol << " and code " << code "." << endl;
  cout << "Outcome: " << passorFail << endl << endl;
  cout << "The highest mark was " << maxIndex << " and the lowest mark was " << minIndex << "." << endl;
  cout << " ******************************************************************************************************************* " << endl;
#endif  // UNHIDE_OLD_CODE
}

int main()
{
  string name;
  string school;
  studentDetails(name, school);
  getMarks();

  double avgMark = calcAverageYearMark();
  cout << "Your averge mark is " << avgMark << endl;

  double minValue, maxValue;
  int minIndex, maxIndex;
  minMax(minValue, maxValue, minIndex, maxIndex);
  cout << "Your lowest mark was " << minValue << endl;
  cout << "Your highest mark is for " << maxValue << endl;

  Display(name, school);
}
@dhayden
Code 1 = FF = 0-29%
Code 2 = F = 30 - 39%
Code 3 = E = 40-49%
Code 4 = D = 50-59%
Code 5 = C = 60-69%
Code 6 = B = 70-79%
Code 7 = A = 80-100%

Professor still hasn't replied to any of my emails.

@seeplus while the output looks really good I got a few questions. We havn't done "is_arithmetic_v" yet so I don't know what that is , I just read a bit about it and the code has a few errors for me and won't compile

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
12|error: 'size' is not a member of 'std'|
12|note: suggested alternative: 'tie'|
14|error: size of array is not an integral constant-expression|
28|error: 'is_arithmetic_v' is not a member of 'std'|
28|note: suggested alternative: 'is_arithmetic'|
28|error: 'is_arithmetic_v' is not a member of 'std'|
28|note: suggested alternative: 'is_arithmetic'|
28|error: template argument 1 is invalid|
28|error: expected identifier before ',' token|
28|error: expected unqualified-id before ',' token|
In function 'void getMarks(int*)':|
85|error: 'getInp' was not declared in this scope|
85|note: suggested alternative: 'getenv'|
||=== Build failed: 8 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
You need to compile as C++17.
With grades/codes etc then possibly.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <type_traits>
#include <charconv>
#include <iterator>
#include <utility>

constexpr int MinMark { 0 };
constexpr int MaxMark { 100 };
constexpr int Dist { 75 };
constexpr int Pass { 50 };
constexpr const char* subjects[] { "English", "Mathematics", "Life Orientation", "History", "Computer Literacy", "Geography" };
constexpr size_t NOMARKS { std::size(subjects)};

using Marks = int[NOMARKS];
using std::cout;
using std::cin;
using std::left;
using std::setw;
using std::setfill;
using namespace std::string_literals;

struct MinMax {
	int minMark {};
	int maxMark {};
};

struct CodeSymbol {
	std::string symbol;
	size_t code {};
};

template<typename T = int>
typename std::enable_if_t< std::is_arithmetic_v<T>, T>
getInp(const std::string& prm, T mn, T mx) {
	T num {};

	for (std::string inp;;) {
		std::cin.clear();
		std::cout << prm;

		if (std::getline(std::cin, inp) && !inp.empty()) {
			const auto first { std::find_if(inp.begin(), inp.end(), [](unsigned ch) {return !std::isspace(ch); }) };
			const auto end { std::find_if(inp.rbegin(), inp.rend(), [](unsigned ch) {return !std::isspace(ch); }).base() };
			const auto res { std::from_chars(std::to_address(first), std::to_address(end), num) };

			if (res.ec == std::errc {} && res.ptr == std::to_address(end))
				if (num >= mn && num <= mx)
					return num;
				else {
					std::cout << "Value not in range " << mn << " - " << mx << '\n';
					continue;
				}
		}

		std::cout << "Invalid input. Not a(n) " << typeid(T).name() << '\n';
	}
}

const char* yesNo(bool yn) {
	static constexpr const char* yesno[2] {"no", "yes"};

	return yesno[yn];
}

MinMax minMax(const Marks marks) {
	int mn { marks[0] }, mx { marks[0] };

	for (size_t i { 1 }; i < NOMARKS; ++i) {
		if (marks[i] < mn)
			mn = marks[i];

		if (marks[i] > mx)
			mx = marks[i];
	}

	return { mn, mx };
}

double calcAverageYearMark(const Marks marks) {
	double sum {};

	for (size_t i {}; i < NOMARKS; ++i)
		sum += marks[i];

	return sum / NOMARKS;
}

void getMarks(Marks marks) {
	for (size_t m {}; m < NOMARKS; ++m)
		marks[m] = getInp("Please enter your mark for "s + subjects[m] + ": "s, MinMark, MaxMark);
}

void studentDetails(std::string& name, std::string& school) {
	cout << "Please key in your name: ";
	std::getline(std::cin, name);

	cout << "Please key in the name of your school: ";
	std::getline(std::cin, school);
}

const char* passOrFail(double mark) {
	static constexpr const char* pf[2] { "Fail", "Pass" };

	return pf[mark >= Pass];
}

bool awardDistinction(double mark) {
	return mark >= Dist;
}

CodeSymbol codeSymbol(double mark) {
	static constexpr std::pair<unsigned, const char*> symbols[] { {80, "A"}, {70, "B"}, {60, "C"}, {50, "D"}, {40, "E"}, {30, "F"}, {0, "FF"}};

	CodeSymbol cs {};
	size_t indx {};

	for (const auto& [m, s] : symbols)
		if (mark >= m) {
			cs.symbol = s;
			break;
		} else
			++indx;

	cs.code = 7 - indx;
	return cs;
}

#define LINE cout << setw(110) << setfill('*') << "*" << setfill(' ') << '\n'

void display(const Marks marks, const std::string& name, const std::string& school) {
	cout << '\n';
	LINE;
	cout << std::right << setw(66) << "STUDENT ACADEMIC RECORD" << '\n';
	LINE;
	cout << "Name : " << name << std::right << setw(66) << "School : " << school << "\n\n";
	cout << left << setw(30) << "Subject" << setw(8) << "Mark" << setw(8) << "Dist" << setw(8) << "P/F" << setw(8) << "Symbol" << setw(8) << "Code" << '\n';

	for (size_t m {}; m < NOMARKS; ++m) {
		CodeSymbol codsym { codeSymbol(marks[m]) };

		cout << left << setw(30) << subjects[m] << setw(8) << marks[m] << setw(8) << yesNo(awardDistinction(marks[m]));
		cout << setw(8) << passOrFail(marks[m]) << setw(8) << codsym.symbol << setw(8) << codsym.code << '\n';
	}

	const auto avg { calcAverageYearMark(marks) };
	const auto mnx { minMax(marks) };

	cout << "\nThe average mark is " << avg << '\n';
	cout << "The minimum mark is " << mnx.minMark << '\n';
	cout << "The maxmimum mark is " << mnx.maxMark << '\n';
	cout << "Overall distinction: " << yesNo(awardDistinction(avg)) << '\n';
	cout << "Overall " << passOrFail(avg) << '\n';
	//cout << " Average Year Mark: " << average << "with Symbol " /*symbol*/ << " and code " << /*code*/ "." << endl;
	//cout << "Outcome: " << passOrFail << endl << endl;
	LINE;
}

int main() {
	std::string name, school;
	Marks marks;

	studentDetails(name, school);
	getMarks(marks);
	display(marks, name, school);
}



Please key in your name: ewewe
Please key in the name of your school: fdfdfd
Please enter your mark for English: 67
Please enter your mark for Mathematics: 98
Please enter your mark for Life Orientation: 99
Please enter your mark for History: 78
Please enter your mark for Computer Literacy: 67
Please enter your mark for Geography: 99

**************************************************************************************************************
                                           STUDENT ACADEMIC RECORD
**************************************************************************************************************
Name : ewewe                                                         School : fdfdfd

Subject                       Mark    Dist    P/F     Symbol  Code
English                       67      no      Pass    C       5
Mathematics                   98      yes     Pass    A       7
Life Orientation              99      yes     Pass    A       7
History                       78      yes     Pass    B       6
Computer Literacy             67      no      Pass    C       5
Geography                     99      yes     Pass    A       7

The average mark is 84.6667
The minimum mark is 67
The maxmimum mark is 99
Overall distinction: yes
Overall Pass
**************************************************************************************************************

Last edited on
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>
#include <iomanip>

using namespace std;

void studentDetails(string&, string&, string&);
void getMarks(double[], string[], int);
double calcAverageYearMark(double[], int);
void minmaxIndex(double[], int, int&, int&);

bool passOrFail(int);
bool distinction(int);
void getCode(int, int&, string&);

void displayReport(string, string, string, double[], string[], int);

int main()
{
    const int NO_SUBJECTS{6};
    double marks[NO_SUBJECTS]{0};
    string subject[NO_SUBJECTS]
    {
        "English","Mathematics","Life Orientation","History",
        "Computer Literacy","Art"
    };
    
    
    string name, surname, school;
    studentDetails(name, surname, school);
    getMarks(marks, subject, NO_SUBJECTS);
    displayReport(name, surname, school, marks, subject, NO_SUBJECTS);
    
    return 0;
}

void studentDetails(string& nm, string& sn, string& sc)
{
    cout << "             Please key in your name: ";
    std::getline(std::cin, nm);
    
    cout << "          Please key in your surname: ";
    std::getline(std::cin, sn);
    
    cout << "Please key in the name of your school: ";
    std::getline(std::cin, sc);
}


void getMarks(double mks[], string subj[], int no_subj)
{
    for(int i = 0; i < no_subj; i++)
    {
        cout << "Key in your mark for " << subj[i] << ": ";
        cin >> mks[i];
    }
}


double calcAverageYearMark(double mks[], int no_subj)
{
    double totMark{0};
    
    for(int i = 0; i < no_subj; i++)
    {
        totMark += mks[i];
    }
    
    return totMark/no_subj;
}


void minmaxIndex(double mks[], int no_subj, int& mini, int& maxi)
{
    double min{1000};
    double max{-1};
    
    for (int i = 0; i < no_subj; ++i)
    {
        if (mks[i] < min)
        {
            min = mks[i];
            mini = i;
        }
        
        if (mks[i] > max)
        {
            max = mks[i];
            maxi = i;
        }
    }
}

bool passOrFail(int mks)
{
    if(mks >= 50)
        return true;
    else
        return false;
}

bool distinction(int mks)
{
    if(mks >= 75)
        return true;
    else
        return false;
}

void getCode(int mks, int& code, string& grade)
{
    if(mks > 80)
    {
        code = 7;
        grade = "A";
    }
    else if( mks > 70)
    {
        code = 6;
        grade = "B";
    }
    else if(mks > 60)
    {
        code = 5;
        grade = "C";
    }
    else if(mks > 50)
    {
        code = 4;
        grade = "D";
    }
    else if(mks > 40)
    {
        code = 3;
        grade = "E";
    }
    else if(mks > 30)
    {
        code = 2;
        grade = "F";
    }
    else
    {
        code = 1;
        grade = "FF";
    }
}

void displayReport(string nm, string sn, string sc, double mks[], string subj[], int no_subj)
{
    cout
    << "REPORT FOR:\n"
    << "  NAME: " << nm << " SURNAME: " << sn
    << " SCHOOL: " << sc << "\n\n";
    
    int code{0};
    string grade;
    
    for(int i = 0; i < no_subj; i++)
    {
        cout
        << setw(3) << i << ' ' << setw(20)  << subj[i] << '\t' << mks[i] << '\t';
        if( passOrFail(mks[i]) )
            cout << "Pass";
        else
            cout << "Fail";
        
        if( distinction( mks[i]) )
            cout << " Distinction";
        
        getCode(mks[i], code, grade);
        cout << " Code: " << code << " Grade: " << grade;
        
        cout << '\n';
    }
    
    double average_mark = calcAverageYearMark(mks, no_subj);
    cout << "Average marks: " << average_mark;
    
    if( distinction( average_mark ) )
        cout << " Distinction";
    
    
    
    cout << '\n';
    
    int min_index{0};
    int max_index{0};
    minmaxIndex(mks, no_subj, min_index, max_index);
    cout << "Min marks: " << mks[min_index] << '\n';
    cout << "Max marks: " << mks[max_index] << '\n';
}



             Please key in your name: ww
          Please key in your surname: ee
Please key in the name of your school: rr
Key in your mark for English: 99
Key in your mark for Mathematics: 24
Key in your mark for Life Orientation: 56
Key in your mark for History: 76
Key in your mark for Computer Literacy: 83
Key in your mark for Art: 82
REPORT FOR:
  NAME: ww SURNAME: ee SCHOOL: rr

  0              English	99	Pass Distinction Code: 7 Grade: A
  1          Mathematics	24	Fail Code: 1 Grade: FF
  2     Life Orientation	56	Pass Code: 4 Grade: D
  3              History	76	Pass Distinction Code: 6 Grade: B
  4    Computer Literacy	83	Pass Distinction Code: 7 Grade: A
  5                  Art	82	Pass Distinction Code: 7 Grade: A
Average marks: 70
Min marks: 24
Max marks: 99
Program ended with exit code: 0
We havn't done "is_arithmetic_v"


As posted, that is a general input routine with input type and limit check that will work with different specified types of input. As only int type input is required here, then the getinp() function starting with the template< line can be replaced with the slightly less complicated (still requires C++17):

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
using T = int;
T getInp(const std::string& prm, T mn, T mx) {
	T num {};

	for (std::string inp;;) {
		std::cin.clear();
		std::cout << prm;

		if (std::getline(std::cin, inp) && !inp.empty()) {
			const auto first { std::find_if(inp.begin(), inp.end(), [](unsigned ch) {return !std::isspace(ch); }) };
			const auto end { std::find_if(inp.rbegin(), inp.rend(), [](unsigned ch) {return !std::isspace(ch); }).base() };
			const auto res { std::from_chars(std::to_address(first), std::to_address(end), num) };

			if (res.ec == std::errc {} && res.ptr == std::to_address(end))
				if (num >= mn && num <= mx)
					return num;
				else {
					std::cout << "Value not in range " << mn << " - " << mx << '\n';
					continue;
				}
		}

		std::cout << "Invalid input\n";
	}
}


Validating numeric input in C++ isn't straightforward if you deal not just with range boundaries but also the 'wrong' type of input such as a non-numeric char being entered when a numeric is required etc.
Last edited on
Here's a different numeric validation routine that doesn't require C++17. It uses std::stringstream (and std::string).

http://lb-stuff.com/user-input

This works for base 10 numeric input, including scientific notation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <sstream>
#include <string>

int main()
{
   std::string line;
   double      num { };

   //split onto multiple lines for readability
   while ((std::cout << "Please enter a number: ") //make sure the user knows what you expect
          && std::getline(std::cin, line)) //this loop runs until we break out of it
   {
      std::istringstream is { line };

      if ((is >> num) && !(is >> line)) //re-using `line` to test for extra stuff after the number
      {
         break; //done, we got what we wanted
      }
      std::cerr << "Invalid input, try again.\n";
   }

   std::cout << "Input received: " << num << '\n';
}
Please enter a number: 1.234e17
Input received: 1.234e+17
Thanks all , you've all been helpful and I learned a lot
Topic archived. No new replies allowed.