Expected unqualified-id error on "for" loop line

//Function Declarations
void placeMarker();
void checkWin();
void board();

//Value Declarations
std::string square[9];
bool playerWon;

for (int count=0; count < 10; ++count)
square[count] = " ";

int main()
{

Error on the "for" loop line saying Expected unqualified-id
Googling didn't help. People just kept solving the issue and giving fixed code rather than explaining what the issue was.
The code is meant to assign the same value to every array string because assigning during initilization didn't seem to work.
Last edited on
You must stick everything in a function.

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
#include <stuff>

// Variables
std::string board[3][3];
char current_player = 'X';

void initializeBoard()
{
  for (int r = 0; r < 3; r++)
  for (int c = 0; c < 3; c++)
    board[r][c] = ' ';
}

void placeMarker()
{
}

bool isGameOver()
{
}

bool isDraw()
{
}

void drawBoard()
{
}

int main()
{
  initializeBoard();
  ...

  while (!isGameOver())
  {
    ...
  }

  if (isDraw()) std::cout << "Draw!\n";
  else          std::cout << "Player " << current_player << " has won!\n";
}

Hope this helps.
Please give complete code; sometimes errors are knock-on effects from something before. Put your code in code tags.

At the moment, your for loop appears to be outside any function. Did you intend it to be in main()?

You are also going to try to set square[9], which doesn't exist, because your array has 9 elements counting from 0. However, that won't cause the error that you cite.
Last edited on
lastchance,

Sorry bout the no code tags. Couldn't figure that out. I clicked on the code thing in the format section but it did nothing.

I meant for it to exist outside a function but I was obviously wrong and didn't know it had to be in one.

I copied some code from a C++ tutorial site and it counted 1 higher than I needed. Their code didn't have the error so I was trying to compare the two code lines. I then deleted my code to see if putting his code in its place resolved the issue which i didn't which meant that my code was written right. It was just something else, in this case, placement.
I meant for it to exist outside a function but I was obviously wrong and didn't know it had to be in one.

What would it even mean for that code to be outside any function? When would it execute? How would the compiler know when that code was suppose to run?
I clicked on the code thing in the format section but it did nothing.
Edit your post and try it again; it's bugged when originally creating the topic, but works during editing.
Topic archived. No new replies allowed.