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.
#include <iostream>
#include <cstdlib>
#include <string>
usingnamespace 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";
}
elseif (x==2){
cout << "Welcome Earthlings! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
elseif (x==3){
cout << "Welcome Martians! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
elseif (x==4){
cout << "Welcome Ionions! You are God of your planet. You must ";
cout << "design a species that will conquer the solar system.\n";
}
elseif (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;
}
}
elseif (y==2){
cout<<"you have provided warmth to your civilization.\n";
cout <<"your civilization nestles under fire.\n";
}
elseif (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;
struct PlayerInformation {
int score;
string Name;
};
constint 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)
}
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.
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++){
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.
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[].
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>
usingnamespace std;
struct PlayerInformation
{ int score;
string Name;
};
constint 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).
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:
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
}
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";
}