Harris,there are some mistakes in your logic.I just corrected your code.try this
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
ofstream outfile;
int main()
{
outfile.open("output.txt");
int magicnumber[19][19] ={0}, n;
int lastX=0,lastY=0;
outfile << "Welcome to the Magic Square Program!" << endl << endl;
cout << "Welcome to the Magic Square Program!" << endl << endl;
cout << "Enter an odd digit: ";
cin >> magicnumber[19][19];
outfile << "Number user entered: " << magicnumber[19][19] << endl;
if (magicnumber[19][19] % 2 == 0){
do{
cout << "Please input odd integers only: " << endl;
cin >> magicnumber[19][19];
} while (magicnumber[19][19] % 2 == 0);
}
cout << "Magic Box#: " << " > " << magicnumber[19][19] << " <" << endl << endl;
outfile << "Magic Box#: " << " > " << magicnumber[19][19] << " <" << endl << endl;
cout << "Now enter the size of the array: ";
cin >> n;
int row = 0;
int col = (n / 2);
for (int num = 1; num <= n*n; num++){
if (row < 0)
row = n - 1;
if (col > n - 1)
col = 0;
if (magicnumber[row][col] == 0){
magicnumber[row][col] = num;
}
else
{
magicnumber[lastX + 1][lastY ] = num;
row = lastX + 1;
col = lastY;
}
lastX = row;
lastY = col;
row--;
col++;
}
for (int row = 0; row < n; row++){
for (int col = 0; col < n; col++){
cout << setw(3) << magicnumber[row][col];
outfile << setw(3) << magicnumber[row][col];
}
cout << endl;
outfile << endl;
}
system("pause");
return 0;
}