Advertisement

Char Arrays?

Started by April 08, 2000 12:23 AM
4 comments, last by Peddler 24 years, 7 months ago
I am beginning to work on the highscore system for my game. I am going to have 10 highscores, the scores will be held in a plain array int Score[10]...my question is... Is there a way I could store the names for the scores in a char array to coorespond with the score integers? So I could access Score[1] and then Name[1] to get the score and name. Any help would be appreciated. Thanks -Tim
I just finished a game that does something very similair. Yes you can use character arrays. The way I did it was to have a struct for a high score that has 2 things in it:
char name[MAX_NAME_LENGTH];
int score;
Then I defined 10 of those structs like this :
(structure name) high_score[10];
To acces the name and score of the first high score you would just do :
high_score[0].name = whatever
high_score[0].score = whatever.
Is there anything more specific that you need to know?

*** Triality ***
*** Triality ***
Advertisement
although zerwit's approach is probably nicer and cleaner, you can access strings in an array that way:

char* ppNames[10];
for(int i=0; i<10; i++)
ppNames = (char*) malloc(256);

then you can just set the strings in the array thru plain calls like
strcpy(ppNames[0], "hiscore");

and don't forget to free the array when you don't need it any more

i hope the code is not going to be too screwed up
ridcully


Edited by - Ridcully on 4/8/00 12:55:48 PM
int Score[10];
char Name[10][MAX_NAME_LENGTH + 1];


Visit our homepage: www.rarebyte.de.st

GA
Visit our homepage: www.rarebyte.de.stGA
I agree with Ridcully/Zerwit:

struct _HiScore{    char Name[16];    int Score;} HiScore[10]; 


Ciao
A safer path:

struct HighScore
{
std::string name;
int score;
}hscore[10];

hscore[0].name = "some string";
hscore[0].score = 100;
"after many years of singularity, i'm still searching on the event horizon"

This topic is closed to new replies.

Advertisement