high score table

Hello. I was wondering how I could adapt a high score table to a game that I am in the process of developing. Basically I have playername as a string and the score is saved under int.

However I have no idea how I could save the score in a table.

Here's a sample of my code:

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  #include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

string playername;

int main ()
{
int highscore;

//to game over,repeat:
int k;

//to enter brahman:
int s;

//to conquer space:
int r;

//to use nukes:
int o;

int p;
int t;
int q;

int x;
int y;
int z;
int a;
int b;
int c;
int d;
int e;
int f;

//introduction
cout<<" \n";
cout<<"#####   #####   ##### \n";
cout<<"#   #   #   #   #     \n";
cout<<"#####   #   #   ##### \n";
cout<<"#       #   #   #     \n";
cout<<"#       #####   ##### \n";
cout<<" \n";

cout << "Hello. Welcome to Planets of Evolution!\n";


repeat:

cout<<"enter your player name: ";
cin >>playername;
cout<<" \n";

cout << "Choose your planet: \n";
cout << "1. Venus\n";
cout << "2. Earth\n";
cout << "3. Mars\n";
cout << "4. Jupiter\n";
cout << "5. Pluto\n";
cout << "Type its number and then press Enter: ";
cin >> x;

cout<<"#       #\n";
cout<<" #     #\n";
cout<<"  #   #\n";
cout<<"   # #\n";
cout<<"    #\n";

if (x==1){
cout << "Welcome Venetians! You are God of your planet. You must design ";
cout << "a species that will conquer the solar system.\n";
}
else if (x==2){
cout << "Welcome Earthlings! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
else if (x==3){ 
cout << "Welcome Martians! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
else if (x==4){
cout << "Welcome Ionions! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
else if (x==5){
cout << "Welcome Plutonians! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
else { 
cout << "exit. \n";
system("PAUSE");
return 0;
}
cout <<"Your points: " << x << '\n';

cout <<"Let's Get Ready to EVOLVE!!\n";

//y

cout <<"Upon finding a promising primitive race, you make your first ";
cout <<"decision. Choose:\n";
cout <<"1. kill\n";
cout <<"2. give love\n";
cout <<"3. give technology\n";

cout <<"pick a number: ";
cin >> y;

cout<<"#       #\n";
cout<<" #     #\n";
cout<<"  #   #\n";
cout<<"   # #\n";
cout<<"    #\n";

if (y==1){
cout<<"you've killed your only promising species.\n";
cout <<"better luck next time.\n";
highscore=x+y;
cout<<"your TOTAL score: "<<highscore<<endl;

cout<<"#       #\n";
cout<<" #     #\n";
cout<<"  #   #\n";
cout<<"   # #\n";
cout<<"    #\n";

cout<<"type 1 to play again or 2 to exit: \n";
cin>>k;

switch(k)
{
case 1:
goto repeat;
case 2:
cout<<"ok. game over!\n";
system("PAUSE");
return 0;
default:
cout<<"whatever.\n";
system("PAUSE");
return 0;
}
}
else if (y==2){
cout<<"you have provided warmth to your civilization.\n";
cout <<"your civilization nestles under fire.\n";
}
else if (y==3){
cout<<"you have provided means to escape life's hardships.\n";
cout << "your civilization is at peace.\n";
}
else {
cout<<"exit.\n";
system("PAUSE");
return 0;

}
cout <<"your points: " << x+y << endl;


//z

cout << "your planet shows signs of intelligence. what next?\n";
cout <<"1. build them a temple.\n";
cout <<"2. build a castle for yourself.\n";
cout <<"pick a number: ";
cin >> z;

cout<<"#       #\n";
cout<<" #     #\n";
cout<<"  #   #\n";
cout<<"   # #\n";
cout<<"    #\n";

if (z==2&&y==3&&x==5){
cout<<"your civilization freezes to death.\n";
cout<<"game over.\n";
highscore=z+x+y;
cout<<"your score: "<<highscore<<endl;



Didn't anyone ever tell you that one character long variable names is a terrible ideas?


If you want a score system that only exists in memory (ie, will not persist through multiple program executions), then use a std::map<Player, Score>.

If you want your score table to persist, then I suggest creating a very basic database.
You could also use an array of structs.

Variable declaration:
1
2
3
4
5
6
7
struct PlayerInformation {
  int score;
  string Name;
};
const int HiscoreMax = 10;

PlayerInformation HighscoreTable[HiscoreMax]; //Store the last 10 Highscores since Program execution 


Example of an assignment and printing the table
1
2
3
4
5
6
7
8
9
10
11
12
HighscoreTable[index].Name = playername;
HighscoreTable[index].score = highscore;

[...]

//Printing HighscoreTable's entries one by one
cout << "---Highscores---\n";
cout << "   NAME   SCORE\n";
for(int i=0; i < HiscoreMax; i++){
  cout << HighscoreTable[i].Name << "    " << HighscoreTable[i].score << "\n";
  //This could be made better but it will do it. (right amount of spaces between the name and score)
}


I hope this is helpful to you.
Last edited on
could you please give a sample of a std::map<Player, Score> command being used in my game?

thanks.
which libraries do i need to include to use the [index]?

thanks for writing out the highscores table!
ok i've laid the code out as follows:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

string playername;

HighscoreTable[index].Name=playername;
HighscoreTable[index].score=highscore;

struct PlayerInformation;
{
int score;
string Name;
}


int main ()
{


int HighscoreMax;

int highscore;

//to game over,repeat:
int k;

...

if (y==1){
cout<<"you've killed your only promising species.\n";
cout <<"better luck next time.\n";
highscore=x+y;
cout<<"your TOTAL score: "<<highscore<<endl;

cout<<"------Highscores-------\n";
cout<<"playername score\n";
for (int i=0; i < HighscoreMax; i++){
cout<<HighscoreTable[i].playername<<" "<<HighscoreTable[i].score;
}

here i have the high score table being presented after the total score is added.

the problem is that the compiler (g++) is not declaring the index, the HighscoreTable, and the highscore in lines 11 and 12.

how do i fix this?



Last edited on
i've tested a few different alternatives but now my compiler complains that at line 9 and 10 the HighscoreTable does not have a type??

what's the cause of this?
Perhaps it's because you have no variable named HighscoreTable in your code.
here's the start of my code:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

string playername;

char HighscoreTable[index].Name=playername;
int HighscoreTable[index].score=highscore;

int main ()
{


struct PlayerInformation;
{
int score;
string Name;

}

const int HighscoreMax=10;


PlayerInformation HighscoreTable[HighscoreMax];


...

if (y==1){
cout<<"you've killed your only promising species.\n";
cout <<"better luck next time.\n";
highscore=x+y;
cout<<"your TOTAL score: "<<highscore<<endl;

cout<<"------Highscores-------\n";
cout<<"playername score\n";
for (int i=0; i < HighscoreMax; i++){
cout<<HighscoreTable[i].playername<<" "<<HighscoreTable[i].score;
}


now the compiler's saying:
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
for (int i=0; i < HighscoreMax; i++){

what's wrong with the code?
Last edited on
i understand that

HighscoreTable[index].Name=playername;
and
HighscoreTable[index].score=highscore;

are picking out information relating to playername and highscore and storing it in an index?

however, HighscoreTable is not declared says the compiler. should i declare it as char or int or what?

also there's the question of the [index]. what is it exactly? how does it affect my program? with what do i declare it?
You need to put the declaration of struct PlayerInformation before the first use of it.

In HighscoreTable[index] the "index" is the position in the Array "HighscoreTable".
This was just an example. You would have to adapt the variable to your needs.

This would be the correct declaration:
1
2
3
4
5
6
7
struct PlayerInformation {
  int score;
  string Name;
};
const int HiscoreMax = 10;

PlayerInformation HighscoreTable[HiscoreMax];


This gives you a Highscore Table with a maximum of 10 entries.
Last edited on
@MacC, I did what you advised but the compiler is still complaining that HighscoreTable on lines 9 and 10 does not name a type.

what does this mean?How can I fix it?

Also apparently a ; is missing after struct declaration.
I'm still confused.

Here's my code:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

string playername;

HighscoreTable[index].Name=playername;
HighscoreTable[index].score=highscore;

struct PlayerInformation
{
int score;
string Name;
};

const int HighscoreMax=10;
PlayerInformation HighscoreTable[HighscoreMax];


int main ()
{

---------------------------

the compiler tells me that HighscoreTable doesn't name a type but when I try to declare it the compiler tells me it's already declared under PlayerInformation HighscoreTable[].

could someone please tell me what's going wrong?

thanks.
Last edited on
Lines 9 and 10 are executable code. They need to occur within a function and after you have defined HighscoreTable (line 19).
Lines 9 and 10. index is not defined. You need to define index as an int that contains the value of the element of the table you want to reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
using namespace std;

struct PlayerInformation
{   int score;
    string Name;
};

const int HighscoreMax=10;
PlayerInformation HighscoreTable[HighscoreMax];

int main ()
{  int index = 0;

    HighscoreTable[index].Name = "Fred";
    HighscoreTable[index].score = 100;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


but if i define the HighscoreTable[index].Name="Fred" it won't matter what name the player gives themselves, the highscore table will always give Fred as the player and the score as 100.

i would like to create a table whereby the string that is the playername is saved alongside the highscore that they achieve.

i think it's the struct that's the problem, because when i test the game no players' names are presented and neither are any scores (except "seb" and 800).

my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

struct PlayerInformation
{
  int highscore;
  string Name;
};

const int HighscoreMax=10;
PlayerInformation HighscoreTable[HighscoreMax];

int main ()
{
int index=0;
int highscore;
string Name;

HighscoreTable[index].Name="seb";
HighscoreTable[index].highscore=800;
Last edited on
but if i define the HighscoreTable[index].Name="Fred" it won't matter what name the player gives themselves, the highscore table will always give Fred as the player and the score as 100.

For crying out loud. "Fred" and 100 were just an example to show setting the name and score.

I also didn't take into consideration how many players were in the list or whether the score being added was in the top 10. So let's create an AddHighScore function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
int num_high_scores = 0;
...
void AddHighScore (string name, int score)
{  int low_index;
    if (num_high_scores < HighscoreMax)
    {  // We have less than 10 scores, so simply add this one
        HighscoreTable[num_high_scores].name = name;
        HighscoreTable[num_high_scores].score = score;
        num_high_scores ++;
        return;
    }
    //  Table is full, find lowest score in table 
    low_index = find_lowest_score (... );  // You figure this out
    if (score < HighscoreTable[low_index].score) 
    {  //  score to be added is less than lowest existing score.  Do nothing.
       return;
    }
    // score is >= lowest score, replace the entry
    HighscoreTable[low_index].name = name;
    HighscoreTable[low_index].score = score;
    // Done
}

Last edited on
For crying out loud.

please be patient with me. this is my first attempt at coding.

now, if i do like so:

1
2
HighscoreTable[index].Name=Name;
HighscoreTable[index].highscore=highscore;


the result in the table is a long string of jibberish numbers, e.g.: -1218404352, with neither playername nor highscore.

thank you for the additional function.
Last edited on
i think it's the struct that's the problem, because when i test the game no players' names are presented and neither are any scores (except "seb" and 800).


From your snippet 3 posts back:
15
16
17
18
19
20
int main ()
{   int index=0;

    HighscoreTable[index].Name="seb";
    HighscoreTable[index].highscore=800;
...


You're initializing only the [0] entry in the array. What do you think are in entries [1]-[9]? Hint: garbage.

This is why you have to keep track of how many entries are used in the array verses the size of the array (HighscoreMax). See the use of num_high_scores in the code I provided. If you're using the printing code provided by macC, you need to change the termination condition of the for loop:
1
2
3
    for (int i=0; i < num_high_scores; i++)
    {   cout << HighscoreTable[i].Name << "    " << HighscoreTable[i].score << "\n";
    }
Last edited on
Topic archived. No new replies allowed.