-2

I've read through the questions asked regarding how to do this, but none have worked. I am trying to read in weather temperatures from a file (type doubles). I am supposed to read them using a 2D array (I do not know how many rows, but I know how many columns). After I read them in, I want to display them to see if it worked.

int numRows;
int numCols = 12;
int i = 0;

string line;

while (!inFile.eof()) // Count the number of rows.
{
    getline(inFile, line);
    i++;
}

inFile.close();

numRows = i;
cout << "There are: " << numRows << " rows in this file." << endl;
cout << line << endl;

double TempNum;
double **Temps;
Temps = new double*[numRows];
for (int row = 0; row < numRows; row++)
    Temps[row] = new double[numCols];

while (!inFile.eof())
{
    inFile >> Temps[numRows][numCols];

}

inFile.close();

// Want to print on screen to see if it worked.
for (int row = 0; row < numRows; row++)
{
    for (int col = 0; col < numCols; col++)
        cout << Temps[numRows][numCols];
}

I know looking at it that I am missing something regarding how to get the actual Temperature numbers themselves inside the rows & columns, but I can't figure it out. What am I doing wrong?

Smith John
  • 25
  • 7
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong –  May 12 '18 at 19:48
  • I looked at that question but mine is different. I am reading a 2D array and its a double. That question doesn't cover 2D dyanmically alocated arrays. – Smith John May 12 '18 at 21:01
  • It does cover why `while (!inFile.eof()) ` is wrong. –  May 12 '18 at 21:02

1 Answers1

0

You need to specify a number for the rows. The compiler needs to know how much memory to reserve for your array before execution.

Greg. O Hajdu
  • 106
  • 2
  • 12
  • How would I go about specifying a number for the rows though? Technically speaking, I can use type double for the rows and columns because its temperatures all across. The rows represent the years, while the columns represent the months. I put TempNum in there but I don't know how to include it in the 2d dynamic array. – Smith John May 12 '18 at 21:23
  • Use a `std::vector`, seriously. Even if you want fixed-size arrays, use `std::array` – o11c May 12 '18 at 21:41
  • We haven't learned vectors yet so I wouldn't know how to do that. And why would I scope an array using std? – Smith John May 12 '18 at 21:47