For my c++ class i have to write a code that will read integers in to a 2D array from a file and then, using functions/methods, determine if the array is a magic square. My problem is that when i build or compile i get "error C2664: 'DataIn' : cannot convert parameter 1 from 'std::ifstream' to 'std::fstream &'" Here is the code:
#include <iostream>
#include <fstream>
using namespace std;
void PrintMatrix( int x[][20], int size)
{
for(int i = 0; i < size; ++i)
{
for(int j = 0; j < size; ++j)
{
cout << x[i][j] << "\t";
}
cout << endl;
}
}
int SumRtDiag(int x[][20], int size)
{
int d = 0;
for(int i = 0; i < size; ++i)
{
d += x[i][i];
return d;
}
}
void DataIn(fstream &fin, int x[][20], int size)
{
for(int i = 0; i < size; ++i)
{
for(int j = 0; j < size; ++j)
{
fin >> x[i][j];
}
}
}
int SumCol(int x[][20], int size, int NumCol)
{
int ColSum = 0;
for(int i = 0; i < size; ++i)
{
ColSum += x[i][NumCol];
}
return ColSum;
}
int SumRow(int x[][20], int size, int NumRow)
{
int RowSum = 0;
for(int j = 0; j < size; ++j)
{
RowSum += x[NumRow][j];
}
return RowSum;
}
int FindSize(int x[][20], ifstream fin)
{
int m;
fin >> m;
return m;
}
int SumLftDiag(int x[][20], int size)
{
int sum = 0;
for(int i = 0; i < size; ++i)
{
for(int j = 0; j < size; ++j)
{
j = size - (i + 1);
sum = x[i][j];
}
}
return sum;
}
bool IsMagic(int x[][20], int size)
{
int total;
total = SumRtDiag(x , size);
if(total != SumLftDiag(x, size))
{
for(int i = 0; i < size; ++i)
{
if(SumCol(x, size, i) != total)
return false;
}
for(int j = 0; j < size; ++j)
{
if(SumRow(x, size, j) != total)
return false;
}
}
return false;
return true;
}
int main()
{
ifstream fin("Lab 11.txt");
fin.open("Lab 11.txt");
if(!fin.is_open())
{
cout << "Input error." << endl;
return 0;
}
int x[20][20], size;
size = FindSize(x, fin);
DataIn(fin, x, size);
PrintMatrix(x, size);
for(int i = 0; i < size; ++i)
{
cout << "The sum of row " << i << " equals " << SumRow(x, size, i) << endl;
}
for(int j = 0; j < size; ++j)
{
cout << "The sum of column " << j << " equals " << SumCol(x, size, j) << endl;
}
cout << "The sum of the right diagonal is " << SumRtDiag(x, size) << endl;
cout << "The sum of the left diagonal is " << SumLftDiag(x, size) << endl;
if(IsMagic)
cout << "This array is a magic square." << endl;
else
cout << "This array is not a magic square." << endl;
system("pause");
return 0;
}
Any help would be appreciated!