Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

.cpp[Error] cannot convert 'Achievement' to 'Achievement*' for argument '1' to 'void find_highest(Achievement*)'

–2 votes
asked Aug 21, 2020 by Umar Shamsudin (100 points)
#include<iostream>

using namespace std;

class Achievement

{

string name;

int score;

public:

void set_data(string n, int s)

{

name = n;

score = s;

}

int getScore()

{

return score;

}

void display_scale()

{

cout<<name<<endl;

cout<<"The Class Passing Grade Achievement:"<<score<<"%"<<endl;

if(score>=85 && score<=100)

{

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

}

else if(score<85 && score>=70)

{

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

}

else if(score<70 && score>=60)

{

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

}

else if(score<60 && score>=50)

{

cout<<"* *"<<endl;

}

else

{

cout<<"Poor achievement"<<endl;

}

}

};

void summary(Achievement a[])

{

int dist=0, merit =0, p=0, f=0;

for(int i=0; i<4; i++)

{

if(a[i].getScore()>=85)

{

dist++;

}

else if(a[i].getScore()>=70)

{

merit++;

}

else if(a[i].getScore()>=50)

{

p++;

}

else

{

f++;

}

}

cout<<" SUMMARY "<<endl;

cout<<"Distinction : "<<dist<<" class(es)"<<endl;

cout<<"Merit       : "<<merit<<" class(es)"<<endl;

cout<<"Pass        : "<<p<<" class(es)"<<endl;

cout<<"Fail        : "<<f<<" class(es)"<<endl;

}

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 ob[4];

Achievement A[4];

string name;

int sc;

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);

}

for(int i=0; i<4; i++)

{

ob[i].display_scale();

}

summary(ob);

cout<<"----------------------------------------"<<endl;

    cout<<" Best result "<<endl;

    cout<<"----------------------------------------"<<endl;

    Achievement high = find_highest(A[4]);

    high.display_scale();

}

1 Answer

0 votes
answered Aug 27, 2020 by Peter Minarik (22,320 points)
edited Aug 28, 2020 by Peter Minarik

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 arrays
Element Index01234
Mathematical position12345

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();
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...