c++ grid game

Hi, im making a minesweeper like game and trying to find out if this subroutine's code is correct or if there is any errors in it such as if statements.
Thankyou

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
 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);
      
      {

	Records[Index].MediumGamesWon++;

      }


    
  
    if (Dimension == 24);

  {

    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);
	  
	  {

	    Records
	      [Index].MediumGamesWon++;
	  }
      
      
	     if (Dimension ==24)
	   

	      {

		Records[Index].LargeGamesWon++;

	      }

	  }

	}

    }

}
Your code formatting made what you had difficult to read.

After reformatting it, it wasn't clear what it should be doing. Perhaps if you wrote a definition of what the function is doing, it might be possible comment further. But I'm pretty much stumped right now.

This is my reformated rendition of what you had, without the syntax errors.
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
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)
    {
        //update record
        Records[Index].GamesPlayed++;
        if (Won == true)
        {
            if (Dimension == 8)
                Records[Index].SmallGamesWon++;
            else if (Dimension == 16);
                Records[Index].MediumGamesWon++;

            if (Dimension == 24)
                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);
                        Records[Index].MediumGamesWon++;

                    if (Dimension ==24)
                        Records[Index].LargeGamesWon++;
                }
            }
        }
    }
}
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
thank you for the relpies im getting 2 errors in my entire program and i dont understand where ive gone wrong

https://pastebin.com/fpk2AvC7
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
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
}
^
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
@Ben19, did you try my code?
i tried it and was getting more errors
this is where i am so far need to put proper indentations to make it easier to read but now its been narrowed to 1 error.

https://pastebin.com/ZWC1Sg6W
Topic archived. No new replies allowed.