I redid my code completely, building around a skeleton my teacher provided. The code is below. It does not print to the output file for some reason, unless I take the section starting with
Code:
if (constsum != comparesum)
and ending with
out of the code.
It only prints the magic squares in my input file in proper format, meaning the output file is open correctly. However, it does not print my heading. Can anyone help me out again?
Code:
// Trey Brumley// Johnson 1063
// Purpose: The purpose of this program is to
// Input: squares.txt - 4 5x5 arrays of numbers
// Output: output.txt - Print heading, reprint the arrays and tell
// whether they are magic squares or not.
// Usage: What the command line should look like:
// executable_name param1 param2 �
// Example:
// encrypt input_file output_file
//---------------------------------------------------------------------
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
const int SIZE = 5;
void Heading(ofstream &out);
void GetSquare(int s[][SIZE], ifstream &in);
void PrintSquare(int s[][SIZE], ofstream &out);
int RowSum(int s[][SIZE], int r);
int ColSum(int s[][SIZE], int c);
int D1Sum(int s[][SIZE]);
int D2Sum(int s[][SIZE]);
int main(int argc, char* argv[])
{
ifstream infile;
ofstream outfile;
int square[SIZE][SIZE];
int constsum;
int comparesum;
bool magic;
int x=1, y=1;
Heading(outfile);
infile.open(argv[1]);
if(infile.fail())
{
cout << "Could not open file. \n\n";
system("pause");
return 0;
}
outfile.open(argv[2]);
for (int i = 0; i < 4; i++)
{
GetSquare(square, infile);
constsum = D1Sum(square);
comparesum = D2Sum(square);
if (constsum != comparesum)
magic = false;
else
while (x <= SIZE)
{
comparesum = RowSum(square, x);
if (constsum != comparesum)
magic = false;
else
{
magic = true;
x++;
}
}
while (y <= SIZE)
{
comparesum = ColSum(square, y);
if (constsum != comparesum)
magic = false;
else
{
magic = true;
y++;
}
}
PrintSquare(square, outfile);
if (magic == true)
outfile << "The array above is a magic square with a constant sum = " << constsum << ". \n\n";
else
outfile << "The array above is not a magic square. \n\n";
}
infile.close();
outfile.close();
system("pause");
return 0;
}
//------------------------------------------------------------------------------
// The following function will print out a heading for the hard copy of results.
//------------------------------------------------------------------------------
void Heading(ofstream &out)
{
out << "Trey Brumley" << endl;
out << "CMPS 1063 - Johnson" << endl;
out << "February 8, 2013" << endl;
out << "Program 1 - Magic Squares" << endl;
out << "=========================" << endl << endl;
out << "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;
out << "A magic square is a square of numbers with a constant sum of each of its rows, columns, and diagonals." << endl << endl;
}
//--------------------------------------------------------------------------------------
// The following function will read in numbers into a 2D array, forming a number square.
//--------------------------------------------------------------------------------------
void GetSquare(int s[][SIZE], ifstream &in)
{
for (int r = 0; r < SIZE; r++)
for (int c = 0; c < SIZE; c++)
in >> s[r][c];
}
//-----------------------------------------------------------------
// This function will print the number square in the proper format.
//-----------------------------------------------------------------
void PrintSquare(int s[][SIZE], ofstream &out)
{
for (int r = 0; r < SIZE; r++)
{
for (int c = 0; c < SIZE; c++)
out << setw(3) << s[r][c];
out << endl;
}
}
//-------------------------------------------------------------------------------
// This function will determine and pass back the sum of the numbers in each row.
//-------------------------------------------------------------------------------
int RowSum(int s[][SIZE], int r)
{
int sum = 0;
for(int c = 0; c < SIZE; c++)
sum += s[r][c];
return sum;
}
//----------------------------------------------------------------------------------
// This function will determine and pass back the sum of all numbers in each column.
//----------------------------------------------------------------------------------
int ColSum(int s[][SIZE], int c)
{
int sum = 0;
for(int r = 0; r < SIZE; r++)
sum += s[r][c];
return sum;
}
//-----------------------------------------------------------------------------------------
// This function will determine and pass back the sum of all numbers in the first diagonal.
//-----------------------------------------------------------------------------------------
int D1Sum(int s[][SIZE])
{
int sum = 0;
for(int r = 0; r < SIZE; r++)
{
int c = r;
sum += s[r][c];
}
return sum;
}
//------------------------------------------------------------------------------------------
// This function will determine and pass back the sum of all numbers in the second diagonal.
//------------------------------------------------------------------------------------------
int D2Sum(int s[][SIZE])
{
int sum = 0;
for(int r = 1; r < SIZE+1; r++)
{
int c = 6 - r;
sum += s[r][c];
}
return sum;
}