I am having trouble reading numbers from a file into a 2d array in c++. It reads the first row just fine but the rest of the rows are populated with 0's. I have no idea what I'm doing wrong.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int myarray[20][20];
int totRow = 20, totCol = 20, number, product, topProduct = 0, row, col, count;
char element[4];
ifstream file;
file.open( "c:\\2020.txt" );
if( !file )
{
cout << "problem";
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
while( file.good())
{
for( row = 0; row < totRow; row++ )
{
for( col = 0; col < totCol; col++ )
{
file.get( element, 4 );
number = atoi( element );
myarray[row][col] = number;
cout << myarray[row][col] << " ";
}
cout << endl;
}
file.close();
}
whileloop withstd::copy_n( std::istream_iterator<int>( file ), 20*20, &myarray[0][0] );.