1

This is my last function for my magic square and for some reason it's giving me an error that there is "'[int]' for array subscript" but I don't know what that means, if someone could help explain what I have to do.

bool Square::is_magic()
{
    for (i = 0; i < size-1; i++)
    {
        if (sum_row[i] != sum_row[i+1])
            return false;

    if (sum_col[i] != sum_col[i+1])
        return false;
        }

    if (sum_row[0] != sum_col[0])
        return false;

    if (sum_row[0] != sum_maindiag[0])
        return false;

    if (sum_row[0] != sum_other[0])
        return false;

    return true;
}
false
  • 10,264
  • 13
  • 101
  • 209
Richard
  • 41
  • 5

2 Answers2

4

Ok everybody was beginer at some time. I really recommend you to read one or two books focused on c++. (Personally I learned programming with "Learn c++ in 21 days", many complain but it was good start for me).

And for the code. Not sure that it's what you need, it should go like this:

bool Square::is_magic()
{
   int i;
   for (i = 0; i < size-1; i++)
   {
       if (sum_row[i] != sum_row[i+1])
          return false;

       if (sum_col[i] != sum_col[i+1])
          return false;
   }

   if (sum_row[0] != sum_col[0])
         return false;

   if (sum_row[0] != sum_maindiag[0])
         return false;

   if (sum_row[0] != sum_other[0])
         return false;

   return true;

}

Some comments:

  1. You don't need brackets for 1 command after if,for,while statement

  2. Suggest using if -> else if -> else. Here it doesn't matter because you jump out of function as soon as you find something not correct, but in case you would continue in code you would check other statements even if it wasn't necessary.

  3. Get used to some style, make your own or copy someone's. Personally I use brackets this way:

    if (something != somethingElse){ doSomeNastyThings(); doEvenMore(); }

Good luck..

Edit: added variable declaration int for statement, updated brackets (clever idea as last 3 if-s aren't using index)

Raven
  • 4,783
  • 8
  • 44
  • 75
  • Thank you so much. I'll keep trying, and I trued this, but it gave me the same errors. I hope I can figure out what's wrong. – Richard Feb 12 '11 at 01:10
  • 1
    oh yes.. I overlooked that one.. in for statement.. First part of "for" is initialization so you need to define your variable just like you would normally.. try edited code – Raven Feb 12 '11 at 01:21
  • I just tried that again, and it's still giving me the same error. I see nothing wrong at all in the code, this is the only part that shows an error. – Richard Feb 12 '11 at 02:03
  • now there's an error at << for (i = 0; i < size-1; i++){>> that says that "i" was not declared in this scope. Then every if statement after that says "invalid types '[int]' for array subscript" – Richard Feb 12 '11 at 02:07
  • That's strange. I even tried to compile my code and it's fine so there's problem other than this code. One more thing it could be pretty crappy compiler which works in c-style. There declaration must be done before "for" statement. Try code now. And can you include header for your Square class in your question? thanks. – Raven Feb 12 '11 at 11:13
1

If statements are formatted like this:

if (condition) {
  do_this()
}

not like this:

{
if (condition)
  do_this()
}

They way you're formatting your code you're closing your for loop after two lines, which I imagine is not what you're trying to do (since you're referring to var i afterwards).

Rhys van der Waerden
  • 3,526
  • 2
  • 27
  • 32