Need help with a Highscore program.

Hello, so I'm trying to code a highscore program, with 2 classes and some methods.

But when I try to fill the object[i] with some new numbers and name, it sets to the last one I entered. I have tryed to use the this-> for it but it won't work.

So i wonder if someone can take a look at where I am atm.

Best regards.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "HighScore.h"
#include "HSItem.h"
using namespace std;

int main()
{
    HighScore hs(5);

    hs.Add("test1", 55);
    hs.Add("test2", 56);
    hs.Add("test3", 57);
    hs.Add("test4", 58);
    hs.Add("test5", 59);
    hs.Print();


    return 0;
}


HSItem.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "HighScore.h"
#include "HSItem.h"
using namespace std;

HSItem::HSItem()
{
    //ctor
}


HighScore.cpp
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
#include <iostream>
#include "HighScore.h"
#include "HSItem.h"
using namespace std;

HighScore::HighScore(int _maxInList)
{
    maxInList = _maxInList;
}

void HighScore::Add(char* _name, int _points)
{
    for(int i = 0; i < maxInList; i++)
    {
    scoreBoard[i].name = _name;
    scoreBoard[i].points = _points;
    }
}

void HighScore::Print()
{
    cout << scoreBoard[0].name << endl;
    cout << scoreBoard[1].name << endl;
    cout << scoreBoard[2].name << endl;
    cout << scoreBoard[3].name << endl;
    cout << scoreBoard[4].name << endl;

    cout << scoreBoard[0].points << endl;
    cout << scoreBoard[1].points << endl;
    cout << scoreBoard[2].points << endl;
    cout << scoreBoard[3].points << endl;
    cout << scoreBoard[4].points << endl;
}
The output of this is:

test5
test5
test5
test5
test5
59
59
59
59
59

You declare a HighScore object hs with a maxinList value of 5 at line 8 of main.cpp.

You then call the Add function on hs with test1 data - the Add function loops 5 times and assigns that same test1 data to each element of the scoreboard. Subsequent calls to the Add function from main on hs go through the same for loop and assign the same test data to all elements of the scoreboard.

So when you print, each item still holds the value of the last test data sent to the Add function.

Not enough information. You haven't included your highscore.h or hsitem.h headers.

highscore.cpp line 13: You're initializing every scoreBoard occurrance to the same thing so lines 10-14 in main.cpp all do the same thing.

Higscore.cp line15: This line looks suspicious. How is name declared? _name is a pointer. You can't copy a C-stype string this way unless name is a std::string.

How is the scoreBoard array declared? Relying on a value in the constructor to be the same as the size of the array is asking for trouble.

BTW, it is poor style to use names that begin with an underscore. Names beginning with an underscore are reserved for the compiler.
HSItem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef HSITEM_H
#define HSITEM_H

#include "HighScore.h"

class HSItem
{
    public:
        HSItem();
        char* name;
        int points;
};

#endif // HSITEM_H 


HighScore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef HIGHSCORE_H
#define HIGHSCORE_H
#include "HSItem.h"


class HighScore : public HSItem
{
    public:
        HighScore(int _maxInList);
        HSItem scoreBoard[5];
        int maxInList;
        void Add(char* _name, int _points);
        void Print();
};

#endif // HIGHSCORE_H 
I cant get this to work, started to use vector instead of the regular array[] but it wont work, have I missed something?

Should I not use Inherit-ens and use composition instead?
What you have makes no sense at all. Furthermore, did you change the Add() after you learned from other posts about its logical error?

Please, describe what your program should achieve?
The things I have changed is that I stopped use the char*, using string instead and then I have changed the list of object to a vector instead.

What Im trying to achive is a templete of a highscordboard that can be used in what ever game I make.


Do I need to create a object of HSItem in the Add() everytime I add a new name and points?
Why does HighScore inherit HSItem? Your HighScore object is NOT a HSItem. It HAS an array of HSItems. If it is a HAS relationship, it should not be an inheritance.

If Add is going to accept a single name and points, then you need to keep track of how many HSItems you have added. I suggest you look at the use of std::vector to keep track of your HSItems. i.e. The first time Add is called it should add the [0] entry, then next time the [1] entry, etc.

Your program assumes that Add will be called exactly 5 times. No more, no less. Again, std::vector will give you the flexibility to add as many HSItems as you want.

Topic archived. No new replies allowed.