Hi I am new to this forum and to c++.I am having problems reading data from a txt file into a 2d array. The data is in the file like this
1 5 87
3 3 24
2 4 62
4 2 22
I am only suppose to read in the 3rd column into the array. I am opening the file but the data that I am seeing is not the data in the file.but all calculations on these columns are correct. I don't know how to do the 3rd column part .Here is the code that is giving me problems . Please help.I really need help in getting the data from the file into the 2D array.
for(int i = 0; i <rows; i++)
{
inFile.read(wksales[i], columns);
}
for( i = 0; i < 4;i++)
{
for(int j = 0; j <5;j++)
{
printf("%2d ", wksales[i][j]); //EXAMPLE from book
}
printf("\n");
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int rows=4;
const int columns=5;
char wksales[rows][columns];
ifstream inFile;
inFile.open("c:/data.txt",ios::in);
if (!inFile)
{
cout << "Unable to open file";
exit(1);
}
int j,i;
for( i = 0; i <rows; i++)
{
inFile.read(wksales[j]);
}
for( i = 0; i < 4;i++)
{
for( j = 0; j <5;j++)
{
printf("%2d ", wksales[j]);
}
printf("\n");
}
//sum of each individual column
for (int col=0;col<columns ;col++)
{
int totals=0;
for(int row=0;row<rows ;row++)
totals= totals +wksales[row][col];
cout<<" "<<totals<<" ";
}
inFile.close();
return 0;
}