• Advertisement

Archived

This topic is now archived and is closed to further replies.

Char Arrays?

This topic is 6524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 ***

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I agree with Ridcully/Zerwit:


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


Ciao

Share this post


Link to post
Share on other sites
A safer path:

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

hscore[0].name = "some string";
hscore[0].score = 100;

Share this post


Link to post
Share on other sites

  • Advertisement