What am I doing wrong in my code?

Every time I run it, column sum and row sum are always equal to each other. Please help!
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
#include <iostream>

using namespace std;
int magicSquare[3][3];
int row;
int col;
int rowSum;
int colSum;



int main()
{







while (colSum == rowSum)
{
    int rowSum1 = 0, rowSum2 = 0, rowSum3 = 0;


    for(int row = 0; row < 3; row++)
    {
            rowSum1 += magicSquare[0][row];
            rowSum2 += magicSquare[1][row];
            rowSum3 += magicSquare[2][row];

    }
    rowSum = rowSum1 + rowSum2 + rowSum3;




int colSum1 = 0, colSum2 = 0, colSum3 = 0;

for(int col = 0; col < 3; col++)
    {
            colSum1 += magicSquare[0][col];
            colSum2 += magicSquare[1][col];
            colSum3 += magicSquare[2][col];

    }
    colSum = colSum1 + colSum2 + colSum3;

      for (row = 0; row < 3; row++)
	{
		for (col = 0; col < 3; col++)
		{
			magicSquare[row][col] = rand()%9;
			cout << magicSquare[row][col] << "    ";
		}
        cout << endl;

	}
        cout <<  "Column Sum: " << colSum << endl;
        cout << "Row Sum: " << rowSum << endl;
        cout << endl;
cout << "Not a magic square!" << endl;
cout << endl;
cout << endl;
cout << endl;

}
cout << "A magic square!" << endl;



    return 0;
}
this may not be it but, you have put collum sum equal to row sum. Instead assign it coolum sum equal to row sum.
hope it helps- DOAFtW
I don't think that is it because when I put while(colSum = rowSum) it does nothing and just shows "A magic square!" idk what I'm doing wrong....
Maybe this is what you wanted to do?

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
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
	srand(time(NULL));

	int magicSquare[3][3];
	int row;
	int col;
	int rowSum = 0;
	int colSum = 0;

	while (true)
	{
		for (row = 0; row < 3; row++)
		{
			for (col = 0; col < 3; col++)
			{
				magicSquare[row][col] = rand() % 9;
				cout << magicSquare[row][col] << "    ";
			}
			cout << endl;
		}

		int rowSum1 = 0, rowSum2 = 0, rowSum3 = 0;

		for (int row = 0; row < 3; row++)
		{
			rowSum1 += magicSquare[0][row];
			rowSum2 += magicSquare[1][row];
			rowSum3 += magicSquare[2][row];
		}
		rowSum = rowSum1 + rowSum2 + rowSum3;

		int colSum1 = 0, colSum2 = 0, colSum3 = 0;

		for (int col = 0; col < 3; col++)
		{
			colSum1 += magicSquare[col][0];
			colSum2 += magicSquare[col][1];
			colSum3 += magicSquare[col][2];
		}

		colSum = colSum1 + colSum2 + colSum3;

		cout << "Column Sum: " << colSum << endl;
		cout << "Row Sum: " << rowSum << endl;
		cout << endl;

		if (rowSum == colSum)	break;
	
		cout << "Not a magic square!" << endl;
		cout << endl;
		cout << endl;
		cout << endl;

	} 
	cout << "A magic square!" << endl;

	system("pause");
	
	return 0;
}
Topic archived. No new replies allowed.