May 22, 2020 at 4:39am UTC
Looks like he means something like this. (untested)
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
const int MaxRecords = 100;
void PlayerRecord::set(string PlayerName, int games=0,
int small=0, int med=0, int large=0)
{
Name = PlayerName;
GamesPlayed = games;
SmallGamesWon = small;
MediumGamesWon = med;
LargeGamesWon = large;
}
void PlayerRecord::update_games_won(int Dimension)
{
if (Dimension == 8) ++SmallGamesWon;
else if (Dimension == 16) ++MediumGamesWon;
else ++LargeGamesWon;
}
void UpdateRecords(PlayerRecord Records[MaxRecords],
string PlayerName, int Dimension, bool Won)
{
int Index = 0;
while (Index < MaxRecords && !Records[Index].Name.empty())
{
if (Records[Index].Name == PlayerName) break ;
++Index;
}
if (Index == MaxRecords)
{
cout << "Out of memory, player not created\n" ;
}
else if (!Records[Index].Name.empty())
{
// update record
++Records[Index].GamesPlayed;
if (Won) Records[Index].update_games_won(Dimension);
}
else // player doesn't exist
{
Records[Index].set(PlayerName, 1);
if (Won) Records[Index].update_games_won(Dimension);
}
}
Last edited on May 22, 2020 at 4:40am UTC
May 22, 2020 at 2:34pm UTC
What are the actual error messages? Deciphering them is a good skill to learn.
1 2 3 4 5 6 7
else if (Dimension == 16);
{
Records[Index].MediumGamesWon++;
}
Remove the semi-colon after the "else if (Dimension == 16)".
Last edited on May 22, 2020 at 2:36pm UTC
May 22, 2020 at 2:38pm UTC
the error message is this
main.cpp: In function ‘void UpdateRecords(PlayerRecord*, std::string, int, bool)’:
main.cpp:253:3: error: a function-definition is not allowed here before ‘{’ token
{
^
main.cpp:955:3: error: expected ‘}’ at end of input
}
^
May 22, 2020 at 2:45pm UTC
Issues like that would not happen if you worked on making your indentation consistent.
After every "block" of code, make sure you indent its body.
e.g.
instead of
1 2 3 4 5 6 7 8 9
while (blah)
{
if (blah)
{
blah
}
}
have it be:
1 2 3 4 5 6 7
while (blah)
{
if (blah)
{
blah
}
}
You probably have an extra '{' or '}' somewhere.
This is what your function looks like when properly indented:
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
void
UpdateRecords (PlayerRecord Records[100], string PlayerName, int Dimension,
bool Won)
{
int Index = 0;
bool ExistingPlayer = false ;
while (Records[Index].Name != "" && ExistingPlayer == false && Index < 100)
{
if (Records[Index].Name == PlayerName)
{
ExistingPlayer = true ;
}
else
{
Index++;
}
}
if (ExistingPlayer == true )
{
//update record
Records[Index].GamesPlayed++;
if (Won == true )
{
if (Dimension == 8)
{
Records[Index].SmallGamesWon++;
}
else if (Dimension == 16); // REMOVE THIS ;
{
Records[Index].MediumGamesWon++;
}
if (Dimension == 24); ; // REMOVE THIS ; also make it "else if"
{
Records[Index].LargeGamesWon++;
}
if (Index == 100)
{
cout << "Out of memory, player not created" ;
}
else //create new player
{
Records[Index].Name = PlayerName;
Records[Index].GamesPlayed = 1;
Records[Index].SmallGamesWon = 0;
Records[Index].MediumGamesWon = 0;
Records[Index].LargeGamesWon = 0;
if (Won == true )
{
if (Dimension == 8)
{
Records[Index].SmallGamesWon++;
}
else if (Dimension == 16); // REMOVE THIS ;
{
Records[Index].MediumGamesWon++;
}
if (Dimension == 24) // also make it "else if"
{
Records[Index].LargeGamesWon++;
}
}
}
}
}
Notice that you're missing a closing }.
Also, remove the semi-colons after if statements.
Last edited on May 22, 2020 at 2:52pm UTC
May 22, 2020 at 3:31pm UTC
@Ben19, did you try my code?
May 22, 2020 at 3:45pm UTC
i tried it and was getting more errors