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;
}