Reputation: 293
I am writing code to read in a set of numbers and then display them in a 4x4 grid on screen, the program will then determine if it is a magic square. My question is how can I get the user to input the numbers into a file that is shown in the readData method so that when that method is called it will display the users grid.
Also where does the file data.txt get stored?
thanks
#include<iostream>
#include<fstream>
using namespace std;
const int dimension = 4; // dimension for the array
typedef int Sq[dimension] [dimension]; // declare vector for type names
void ReadData(Sq square ) // read data from file
{ ifstream inFile;
char fileName[13] = "data.txt";
inFile.open (fileName); // open file for reading
for (int r = 0; r < dimension; r++) // row loop
for ( int c = 0; c < dimension; c++) // column loop
inFile >> square[r][c]; // read data into matrix
inFile.close( ); // close the file
}
void Display ( const Sq square ) // display matrix
{ cout << " Magic Square Program " << endl << endl;
for (int r = 0; r < dimension; r++)
{ for ( int c = 0; c < dimension; c++)
{ cout.width(6); //set output width to 6 characters
cout << square[r][c] << "\t "; // display numbers
}
cout << endl;
}
}
bool magicSquare( Sq square) // add rows, columns, and diagonals
{ int firstSum = 0, sum;
bool magic = true;
for (int r = 0; r < dimension; r++) // add 1st column for a comparison
firstSum += square[r][1];
for (int r = 1; r < dimension; r++) // row loop first when adding rows
{ sum = 0;
for ( int c = 0; c < dimension; c++)
sum += square[r][c]; // add row
if ( sum != firstSum) // check for magic failure
return (false); // not magic
}
for ( int c = 0; c < dimension; c++) // column loop first when adding columns
{ sum = 0;
for (int r = 0; r < dimension; r++)
sum += square[r][c]; // add columns
if ( sum != firstSum) // check for magic failure
return (false); // not magic
}
sum = 0;
for (int r = 0; r < dimension; r++)
sum += square[r][r]; // add front diagonal
if ( sum != firstSum) // check for magic failure
return (false); // not magic
sum = 0;
for (int r = 0; r < dimension; r++)
sum += square[r][dimension - r - 1]; // add back diagonal
if ( sum != firstSum) // check for magic failure
return (false); // not magic
else
return (true);
} // end magicSquare function
int main( )
{
Sq square;
ReadData( square); // read data from file
Display ( square); // display matrix
if ( magicSquare(square) ) // check for magic property
cout << "\n This Square is Magic \n " << endl;
else
cout << "\n This Square is Not Magic \n " << endl;
system("Pause");
return(0);
}
Upvotes: 0
Views: 1054
Reputation: 39390
My question is how can I get the user to input the numbers into a file that is shown in the readData method?
Ask politely? Since it's a file on the disk, user has to fill it before the program starts. You could use appropriate ifstream
flag to not create the file if it doesn't exist, and display an error message instead, terminating the program afterwards.
Also where does the file data.txt get stored?
In the current execution path of the binary. This may or may not be the folder where the binary is located.
As a side note, as you are using C++, std::array<>
might be a better fit than C-style arrays.
Upvotes: 0
Reputation: 68074
The easiest way is to make the program take the filename as a command-line argument. Main should really look like this, where argc
is the number of arguments, and argv[]
is and array of char pointers to them (argv[0]
is always the name of the executable). See What does int argc, char *argv[] mean?
int main(int argc, char * argv[])
So then, you do
if (argc == 2)
{
ReadData(square, argv[1]);
...
}
else
...
And ReadData would look like this:-
void ReadData(Sq &square, const std::string &filename) // read data from file
{
ifstream inFile;
inFile.open (filename); // open file for reading
NOTE! you need to take square as a reference parameter (&square
) otherwise your input data just gets ignored.
Upvotes: 2