Problems With The Code
There are a few problems here. Let me quote some important lines from your code:
void find_highest(Achievement A[])
{
int max = 0;
for (int i = 0; i > 4; i++)
{
if (A[i].getScore() > max)
{
max = A[i].getScore();
}
}
}
int main()
{
Achievement A[4];
// ...
Achievement high = find_highest(A[4]);
}
Function Signature not Matched
So, the function find_highest() accepts an array of Achievements, yet you provide it with only one Achievement: the 4th element in the array.
Index Out of Bounds
So, because you try to read the element from the array at position 4, but the array is just 4 element long (available positions: 0th, 1st, 2nd, 3rd) you are going to have memory corruption. indexing in C/C++ (and the majority of the other languages) is 0-based. So position 4, is really not the 4th, but the 5th element in the array (which was declared as 4 long): 0, 1, 2, 3, 4
Indexing arraysElement Index | 0 | 1 | 2 | 3 | 4 |
---|
Mathematical position | 1 | 2 | 3 | 4 | 5 |
---|
Similarly, when you fill the variable op with values, you index out of bounds as you iterate through indexes: 0, 1, 2, 3, 4
for (int i = 0; i < 5; i++)
{
cout << "Enter the class name : ";
cin >> name;
cout << "The class achievement (%) : ";
cin >> sc;
ob[i].set_data(name, sc);
}
You should just do instead:
for (int i = 0; i < 4; i++)
Also, please note that your loop in find_highest() is wrong, as the condition is i > 4, instead of i < 4. So the correct one is same as the above line.
Function With No Return Type
You can see above that find_highest() does not have any return value (it's void). Thus the following line will not work:
Achievement high = find_highest(A[4]);
For this, find_highest() should return the right (the highest) Achievement. If you have learned pointers, I'd return the pointer to this Achievement, or rather a reference as this is C++.
If you are not familiar with them, I would just return the index of it.
Function With too Much Assumption
There is also a small problem with find_highest(). In the implementation of the method, one assumes that the A array is 4 long. If this changes, then the implementation of this function should change too. This makes maintaining the code difficult.
To fight this, you can pass in a 2nd parameter that indicates the length of the array.
Alternatively, since this is C++, you can use std::vector<Achievement> to store your Achievements.
A More C++ Focused Solution
I gave it a go and made your solution more C++ like:
#include <iostream>
#include <stdexcept>
#include <vector>
class Achievement
{
private:
std::string _name;
int _score;
public:
Achievement(std::string name, int score) :
_name(name),
_score(score)
{
if (score < 0 || score > 100)
throw std::runtime_error("Score must be between 0% and 100%");
}
int getScore() const
{
return _score;
}
void displayScale() const
{
std::cout << _name << std::endl;
std::cout << "The Class Passing Grade Achievement: " << _score << "%" << std::endl;
if (_score >= 85)
{
std::cout << "* * * * *";
}
else if (_score >= 70)
{
std::cout << "* * * *";
}
else if (_score >= 60)
{
std::cout << "* * *";
}
else if (_score >= 50)
{
std::cout << "* *";
}
else
{
std::cout << "Poor achievement";
}
std::cout << std::endl;
}
};
static void printClassCount(const std::string & category, int count)
{
std::cout << category<< count << " class";
if (count != 1) std::cout << "es";
std::cout << std::endl;
}
static void showSummary(const std::vector<Achievement> & achievements)
{
int dist = 0;
int merit = 0;
int pass = 0;
int fail = 0;
for (const Achievement & a : achievements)
{
int score = a.getScore();
if (score >= 85) dist++;
else if (score >= 70) merit++;
else if (score >= 50) pass++;
else fail++;
}
std::cout << " SUMMARY " << std::endl;
printClassCount("Distinction : ", dist);
printClassCount("Merit : ", merit);
printClassCount("Pass : ", pass);
printClassCount("Fail : ", fail);
}
static Achievement & find_highest(std::vector<Achievement> & achievements)
{
if (achievements.size() == 0)
throw std::runtime_error("vector is empty");
int maxScore = -1; // We choose a value that is less than the allowed minimum score (0).
Achievement & highestAchievement = achievements[0];
for (Achievement & achievement : achievements)
{
int score = achievement.getScore();
if (score > maxScore)
{
maxScore = score;
highestAchievement = achievement;
}
}
return highestAchievement;
}
int main()
{
const int maxClasses = 4;
std::vector<Achievement> achievements;
std::string name;
int score;
for (int i = 0; i < maxClasses; i++)
{
std::cout << "Enter the class name : ";
std::cin >> name;
std::cout << "The class achievement (%) : ";
std::cin >> score;
achievements.push_back(Achievement(name, score));
}
for (int i = 0; i < maxClasses; i++)
{
achievements[i].displayScale();
}
showSummary(achievements);
std::cout << "----------------------------------------" << std::endl;
std::cout << " Best result " << std::endl;
std::cout << "----------------------------------------" << std::endl;
Achievement & high = find_highest(achievements);
high.displayScale();
}