# Code help

• 02-06-2013
Trey Brumley
Code help
The following code should determine whether or not a square of numbers is a magic square or not. The code debugs and run just fine, but the command prompt opens, asks me how many squares I would like to calculate for, and after I hit enter with a number, it just sits there and does nothing.

Can anyone tell me why it does that? Have I done anything wrong?

Code:

```#include <iostream>#include <fstream> #include <iomanip> using namespace std; const int size = 5; void square (int x[size][size]); void heading(); ifstream infile; ofstream outfile; int main() {     infile.open("input.txt");     outfile.open("output.txt");     int c;     cout << "How many squares would you like to determine?  ";     cin >> c;     cout << endl;     heading;     outfile << "The user of this program has chosen to determine whether " << c << " squares are magic squares." << endl << endl;     int x[size][size];     for (int c = 1; c > 0; c++)     {         for (int d=0; d<size; d++)             for (int e=0; e<size; e++)                 infile >> x[d][e];         square(x);     }     system("pause");     return 0; } // This function writes the heading in the outfile. void heading() {     outfile << "Trey Brumley" << endl;     outfile << "CMPS 1063 - Johnson" << endl;     outfile << "January 29, 2013" << endl;     outfile << "Program 1 - Magic Squares" << endl;     outfile << "=========================" << endl << endl;     outfile << "This program will take a certain number of number squares as determined by the user, and it will compare the sums of each row, column, and diagonal." << endl;     outfile << "A magic square is a square of numbers with a constant sum of each of its rows, columns, and diagonals." << endl << endl;     return; } // This function will test a square one row at a time void square (int x[size][size]) {     infile.open("input.txt");     outfile.open("output.txt");     int sum = 0, sum2 = 0;     int y=1, z=1;     //This and the following loop will determine a sum, which will remain constant.     //A second sum of the next row will be determined, which will repeatedly be tested against the first sum.     //If the sums are equal, the loop will continue to run.  If not, the function will end.     for (y; y <= 5; y++)         sum += x[y][z];         while (z<5)         {             sum2 = 0;             for (y=1; y<=5; y++)                 sum2 += x[y][z];             if (sum2 != sum)             {                 outfile << "This square is not a magic square." << endl;                 return;             }             else                 z++;         }     for (z=1; z<=5; z++)         while (y<=5)         {         sum2 = 0;         sum2 += x[y][z];         if (sum2 != sum)         {             outfile << "This square is not a magic square." << endl;             return;         }         else             y++;         }     sum2 = 0;     //The following loops will test diagonals.     for (z=1; z<=5; z++)     {         y=z;         sum2 += x[y][z];         if (sum2 != sum)         {             outfile << "This square is not a magic square." << endl;             return;         }     }     sum2 = 0;     for (z=1; z<=5; z++)         {             y=6-z;             sum2 += x[y][z];             if (sum2 != sum)             {                 outfile << "This square is not a magic square." << endl;                 return;             }         }     outfile << "This square is a magic square.  The constant sum is " << sum << "." << endl;     return; }```
• 02-06-2013
grumpy
The cause of your problem is the for loop
Code:

`for (int c = 1; c > 0; c++)`
c starts with a positive value, is only ever incremented, so is always positive - so c > 0 is always true. Hence an infinite loop. (Strictly speaking, the program will eventually exhibit undefined behaviour when c overflows, so the loop isn't necessarily infinite, but will iterate at least 32766 times before that happens so your program will appear to hang).

Unrelated to your question, but the line
Code:

`heading;`
does nothing (except evaluate the address of the function named heading). If you want the function to be called, it is necessary to do
Code:

`heading();`
• 02-06-2013
std10093
First of all this is wrong:
Code:

`void square (int x[size][size])`
What you can write is
Code:

`void square (int x[][size], int size)`
or
Code:

`void square (int x[][], int size)`
The compiler ignores the first parameter. Why you didn't an error? Because, int size is a global variable (not good technique, I can explain why if you wish after you resolve your infinite loop!).

Ok, so you have an infinite loop, I guess inside square function.

Also, the index of the arrays, start at zero, not at one! (learn this one and for all! You are not fortran programmer!).

Then I would check if the files really open. On line 42 I would check if the x array (x is not really a good name a suppose, but ok), is really filled up as you expect it is. Then I would fill square function with cout's on lines that are prone to infinite loops and I would run the code with a small amount of data. That way you debug yourself the code and you feel good :D

EDIT: Well obviously, grumpy found the mistake while I was typing, but I think you will be benefited a bit by reading my post :)
• 02-06-2013
Elysia
>>int x[][]
You can't do that. You must specify the outer dimension, so it must be something like
int x[][size]
Where size is some constant expression (eg, a number).
• 02-07-2013
std10093
Correct, so you use my first suggestion :)
• 02-07-2013
Trey Brumley
Okay, so I edited the code. The offending code now looks like this:

for (c; c > 0; c--)

which should work, but now nothing at all is printing. Not even "The user has chosen to determine whether X squares are magic squares," which was printing just fine earlier.
• 02-07-2013
std10093
Hmm.. Can you post a bit more code?
• 02-07-2013
Trey Brumley
Quote:

Originally Posted by std10093
Hmm.. Can you post a bit more code?

Code:

```#include <iostream>#include <fstream>#include <iomanip> using namespace std; const int size = 5; void square (int x[size][size]); void heading(); ifstream infile; ofstream outfile; int main() {     infile.open("input.txt");     outfile.open("output.txt");     int c;     cout << "How many squares would you like to determine?  ";     cin >> c;     cout << endl;     heading();     outfile << "The user of this program has chosen to determine whether " << c << " squares are magic squares." << endl << endl;     int x[size][size];     for(c; c > 0; c--)     {         for (int d=0; d<size; d++)             for (int e=0; e<size; e++)                 infile >> x[d][e];         square(x);     }     system("pause");     return 0; } // This function writes the heading in the outfile. void heading() {     outfile << "Trey Brumley" << endl;     outfile << "CMPS 1063 - Johnson" << endl;     outfile << "January 29, 2013" << endl;     outfile << "Program 1 - Magic Squares" << endl;     outfile << "=========================" << endl << endl;     outfile << "This program will take a certain number of number squares as determined by the user, and it will compare the sums of each row, column, and diagonal." << endl;     outfile << "A magic square is a square of numbers with a constant sum of each of its rows, columns, and diagonals." << endl << endl;     return; } // This function will test a square one row at a time void square (int x[size][size]) {     infile.open("input.txt");     outfile.open("output.txt");     int sum = 0, sum2 = 0;     int y=1, z=1;     //This and the following loop will determine a sum, which will remain constant.     //A second sum of the next row will be determined, which will repeatedly be tested against the first sum.     //If the sums are equal, the loop will continue to run.  If not, the function will end.     for (y; y <= 5; y++)         sum += x[y][z];         while (z<5)         {             sum2 = 0;             for (y=1; y<=5; y++)                 sum2 += x[y][z];             if (sum2 != sum)             {                 outfile << "This square is not a magic square." << endl;                 return;             }             else                 z++;         }     for (z=1; z<=5; z++)         while (y<=5)         {         sum2 = 0;         sum2 += x[y][z];         if (sum2 != sum)         {             outfile << "This square is not a magic square." << endl;             return;         }         else             y++;         }     sum2 = 0;     //The following loops will test diagonals.     for (z=1; z<=5; z++)     {         y=z;         sum2 += x[y][z];         if (sum2 != sum)         {             outfile << "This square is not a magic square." << endl;             return;         }     }     sum2 = 0;     for (z=1; z<=5; z++)         {             y=6-z;             sum2 += x[y][z];             if (sum2 != sum)             {                 outfile << "This square is not a magic square." << endl;                 return;             }         }     outfile << "This square is a magic square.  The constant sum is " << sum << "." << endl;     return; }```