Maintaining list of objects in array

I am having trouble with maintaining a list of objects in an array. I need to maintain an array that holds 5 'GameEntry' objects in my 'GameScore' class that keeps track of the top 5 scores from 'GameEntry'. When calling the 'add' function, it should add to the list if there are less than 5 top scores and replace any if it has a higher score.
The way that I have my code now, it only takes in 3 of the scores and it ignores the other two. It ignores the two lowest scores. I am not sure why it is doing this. Any help/tips will be very appreciated

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
class GameEntry
{
public:
	GameEntry() 
	{ 
		entryName = "", entryScore = 0, entryDate = "00/00/0000"; 
	}
	GameEntry(std::string name, int score, std::string date) 
	{
		entryName = name, entryScore = score, entryDate = date;
	}
	std::string getName()
	{
		return entryName;
	}
	std::string getDate()
	{
		return entryDate;
	}
	int getScore()
	{
		return entryScore;
	}

protected:
	std::string entryName, entryDate;
	int entryScore;
};

class GameScore : GameEntry
{
public:
	GameScore() { gameName = ""; }
	GameScore(std::string game) { gameName = game; }

	void add(GameEntry entry)
	{
		GameEntry tempEntry;
		int count = 0;
		for (int i = 0; i < 5; i++)
		{
			count++;
			if (entry.getScore() > topRanked[i].getScore())
			{
				tempEntry = topRanked[i];
				topRanked[i] = entry;
				break;
			}
		}
		for (int i = count; i < 5; i++)
		{
			topRanked[i] = tempEntry;
			if (i == 4)
			{
				break;
			}
			else
				tempEntry = topRanked[i + 1];
		}
		tempEntry.~GameEntry();
	}
	void print()
	{
		std::cout << "Name: " << gameName << std::endl;
		for (int i = 0; i < 5; i++)
		{
			std::cout << i + 1 << ". " << std::setw(12) << std::left << topRanked[i].getName(); 
			std::cout << std::setw(12) << std::left << topRanked[i].getScore();
			std::cout << topRanked[i].getDate() << std::endl;
		}
	}
protected:
	std::string gameName;
	GameEntry topRanked[5];
};

int main()
{
	GameEntry testEntry ("Bob", 689, "10/17/2019");
	GameEntry testEntryTwo ("Bill", 724, "10/24/2019");
	GameEntry testEntryThree ("Sam",  864, "10/30/2019");
	GameEntry testEntryFour ("John", 1000, "11/10/2019");
	GameEntry testEntryFive("Jane", 875, "11/12/2019");
	GameScore testScore ("Classic Pac-Man");
	testScore.add(testEntry);
	testScore.add(testEntryTwo);
	testScore.add(testEntryThree);
	testScore.add(testEntryFour);
	testScore.add(testEntryFive);
	testScore.print();


	system("pause");

	return 0;
}
It doesn't make sense for GameScore to inherit from GameEntry.

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

class GameEntry {
    std::string entryName, entryDate;
    int entryScore;
public:
    GameEntry() : entryDate("00/00/0000"), entryScore() {}
    GameEntry(std::string name, int score, std::string date)
    : entryName(name), entryDate(date), entryScore(score) {}
    std::string getName() { return entryName; }
    std::string getDate() { return entryDate; }
    int getScore() { return entryScore; }
};

class GameScore {
    static const int MaxSize = 5;
    std::string gameName;
    GameEntry topRanked[MaxSize];
public:
    GameScore(std::string game) : gameName(game) {}

    void add(GameEntry entry) {
        int pos = 0;
        for ( ; pos < MaxSize; ++pos)
            if (entry.getScore() > topRanked[pos].getScore())
                break;
        if (pos < MaxSize) {
            for (int i = MaxSize - 1; i > pos; --i)
                topRanked[i] = topRanked[i - 1];
            topRanked[pos] = entry;
        }
    }

    void print() {
        std::cout << "Name: " << gameName << '\n';
        for (int i = 0; i < MaxSize; ++i)
            std::cout << i + 1 << ". "
                      << std::setw(12) << std::left  << topRanked[i].getName()
                      << std::setw(12) << std::right << topRanked[i].getScore()
                      << "  " << topRanked[i].getDate() << '\n';
    }
};

int main() {
    GameEntry e1("Bob",   689, "10/17/2019"),
              e2("Bill",  724, "10/24/2019"),
              e3("Sam",   864, "10/30/2019"),
              e4("John", 1000, "11/10/2019"),
              e5("Jane",  875, "11/12/2019");

    GameScore gs("Classic Pac-Man");

    gs.add(e1);
    gs.add(e2);
    gs.add(e3);
    gs.add(e4);
    gs.add(e5);

    gs.print();
}

you have the same error than in your «insert data» http://www.cplusplus.com/forum/beginner/262175/
(gonna answer there)

apart from that
class GameScore : GameEntry¿do you know what that means? it is not inheritance

tempEntry.~GameEntry();
the destructor is called automatically when the object goes out of scope, so now it's being called twice
you've just corrupted your memory.
@dutch Thank you for the example! A couple questions, why does it not make sense for me not to use inheritance? Also, is there a specific reason why you used a static const int for MaxSize? Is it better than just using a normal int? Or not even using an int at all and just entering 5 whenever you used it?

@ne555 I just caught the mistake on that. I do not know what that means, but I did not get an error so I think that it's valid syntax? I am just not sure what it is or what it does. As for the destructor, thank you for pointing that out. Is there ever a time where I will actually need to call a destructor? If it is automatically called every time it goes out of scope, I don't really see a reason to have to ever call it.
why does it not make sense for me not to use inheritance?

Let me turn that around. Why do you think it does make sense to use inheritence?

Is the relationship between GameScore and GameEntry the sort of relationship that is best modelled by inheritance? In what way?
regarding class GameScore : GameEntry which is equivalent to class GameScore : private GameEntry
https://isocpp.org/wiki/faq/private-inheritance
Topic archived. No new replies allowed.