I have two programs for school. The first program involves the user entering the number of rows and columns of a matrix, then use the random number generator to fill it, and finally ask the user to enter a file name and save the matrix in it. The second program will ask the user to enter the file name (which will then open the file), read the matrix from the file, and then do some math to the matrix.
I can write all the matrices and math parts, but I have no idea how to read and write to files...can anyone help me? Heres what I have so far for the file parts...
In program 1 where the user enters a filename and writes the matrix to the file
void FileName (int rows, int columns, int Matrix [50][50])
{
std::string filename;
int i, j;
cin.ignore();
std::cout << "What would you like to name the file?";
getline (std::cin, filename);
Its supposed to be a text file (I think...) so I used static_cast to write the integers as characters to the file...I'm not sure if thats okay...
and for program two, where the user enters the filename and reads the matrix from it
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
(Lots of other codes/functions here)
void FileName (int Matrix [50][50])
{
string filename, CRows, CColumns, CNum;
int i, j;
cin.ignore();
cout << "What is the file name you would like to open?";
getline (cin, filename);
ifstream input(filename.c_str());
if (!input) //will tell if the file is open
{
cout << "Cannot open file. /n";
exit (1);
}
input >> CRows;
input >> CColumns;
int Rows = atoi(CRows.c_str());
int Columns = atoi(CColumns.c_str());
cout << "The number of rows you entered was" << Rows;
cout << "\n and the number of columns was" << Columns;
for (i = 0; i < Rows; i++)
{
for (j = 0; j < Columns; j++)
{
input >> CNum;
Matrix [i][j] = atoi(CNum.c_str());
}
}
input.close();
cout << "The matrix you opened was...\n";
Print (Rows, Columns, Matrix);
}
Theres something wrong with all of this, I just have no idea what. My teacher very briefly explained files to us so I have very little background and since I am still very new with all of this I have a hard time understanding most of the help I have found online.
int GetColumn ();
int GetRow ();
void FillMatrix (int rows, int columns, int Matrix [50][50]);
void FileName (int rows, int columns, int Matrix [50][50]);
int main ()
{
int rows, columns;
int Matrix [50][50];
rows = GetRow ();
columns = GetColumn ();
FillMatrix (rows, columns, Matrix);
FileName (rows, columns, Matrix);
system ("pause");
return (0);
}
int GetColumn ()
{
int columns;
cout << "How many columns would you like to enter? Must be less then or equal to 50 \n";
cin >> columns;
if (columns > 51 || columns < 1)
{
cout << "The number of columns must be less then or equal to 50. Please enter new number";
cin >> columns;
}
return (columns);
}
int GetRow ()
{
int rows;
cout << "How many rows would you like to enter? Must be less then or equal to 50 \n";
cin >> rows;
if (rows > 51 || rows < 1)
{
cout << "The number of rows must be less then or equal to 50. Please enter new number";
cin >> rows;
}
return (rows);
}
void FillMatrix (int rows, int columns, int Matrix [50][50])
{
int i, j;
srand ((unsigned) time(NULL));
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
int random = rand();
Matrix [i][j] = random;
}
}
}
void FileName (int rows, int columns, int Matrix [50][50])
{
std::string filename;
int i, j;
cin.ignore();
std::cout << "What would you like to name the file?";
getline (std::cin, filename);
and I'm not completely done with program 2 so there are no calls to the functions in the main...
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void FileName (int Matrix [50][50]);
void Print (int Rows, int Columns, int Matrix [50][50]);
void PrintAverage (int Columns, int ArrayA [50]);
void Average (int Rows, int Columns, int Matrix [50][50], int ArrayA [50]);
int main ()
{
int Matrix [50][50];
int ArrayA [50];
FileName (Matrix);
system ("pause");
return (0);
}
void FileName (int Matrix [50][50])
{
string filename, CRows, CColumns, CNum;
int i, j;
cin.ignore();
cout << "What is the file name you would like to open?";
getline (cin, filename);
ifstream input(filename.c_str());
if (!input) //will tell if the file is open
{
cout << "Cannot open file. /n";
exit (1);
}
input >> CRows;
input >> CColumns;
int Rows = atoi(CRows.c_str());
int Columns = atoi(CColumns.c_str());
cout << "The number of rows you entered was" << Rows;
cout << "\n and the number of columns was" << Columns;
for (i = 0; i < Rows; i++)
{
for (j = 0; j < Columns; j++)
{
input >> CNum;
Matrix [i][j] = atoi(CNum.c_str());
}
}
input.close();
cout << "The matrix you opened was...\n";
Print (Rows, Columns, Matrix);
}
void Print (int Rows, int Columns, int Matrix [50][50])
{
int i, j;
for (i = 0; i < Rows; i++)
{
for (j = 0; j < Columns; j++)
{
cout << Matrix [i][j];
cout << " ";
}
cout << "\n";
}
}
void Average (int Rows, int Columns, int Matrix [50][50], int ArrayA [50])
{
int i, j, sum;
for (j = 0; j < Columns; j++)
{ sum = 0;
for (i = 0; i < Rows; i++)
{
sum = sum + Matrix[i][j];
}
ArrayA [j] = sum / i;
}
PrintAverage (Columns, ArrayA[50]);
}
void PrintAverage (int Columns, int ArrayA [50])
{
int i;
cout << "The averages of each column are \n";
for (i = 0; i < Columns; i++)
{
cout << ArrayA [i];
cout << "\n";
}
}