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.
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.
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.