简体   繁体   中英

how to add to a 2 dimensional array from file contents?

I have several lines of 5 different values each in a file, and I want to create a 2 dimensional array using that file where each array inside the main array has 5 values and I want to add the the values into the array until 20 sub arrays exist in the main array or the end of the file is reached

With my code however, there is no existing output at all and I don't know what happened

#include <iostream>
#include <fstream>

const int row = 20;
const int column = 5;

using namespace std;
int main()
{
    double temperature[row][column];

    double temp;

    ifstream inFile;
    inFile.open("grades1.txt");
    if (inFile) //if the input file to be read open successfully then goes on
    {
        while (inFile >> temp) //reads through file
       {  //adds answers to the array
    // Inserting the values into the temperature array
            for (int i = 0; i < row; i++)
            {
                for(int j = 0; j < column; j)
                {
                    temperature[i][j] = temp;
                }
            }
       }

       for(int i=0; i<row; i++)    //This loops on the rows.
        {
        for(int j=0; j<column; j++) //This loops on the columns
            {
            cout << temperature[i][j]  << "  ";
            }
            cout << endl;
        }
    }
}

This is the temperature file I am using

61.4 89.5 62.6 89.0 100.0
99.5 82.0 79.0 91.0 72.5

If anyone could find the error in my code to fix it, that would be really helpful

With the way you have written your loops, every number read from the file is assigned to all the elements of the array. However, only the last number read will be retained when you exit from the while loop.

The code to read the number needs to be in the innermost loop.

for (int i = 0; i < row; ++i)
{
    for(int j = 0; j < column; ++j)
    {
        if ( inFile >> temp )
        {
           temperature[i][j] = temp;
        }
    }
}

When the end of the file is reached, you would like to be able stop further attempts to read. It will be better to use a function to read the data and return from the function when the EOF is reached or there is any other kind of error in reading the data.

While at it, you might as well use a function to print the data.

#include <iostream>
#include <fstream>

const int row = 20;
const int column = 5;

using namespace std;

int readData(ifstream& inFile, double temperature[row][column])
{
   for (int i = 0; i < row; ++i)
   {
      for(int j = 0; j < column; ++j)
      {
         double temp;
         if ( inFile >> temp )
         {
            temperature[i][j] = temp;
         }
         else
         {
            return i;
         }
      }
   }

   return row;
}

void printData(double temperature[][column], int numRows)
{
   for(int i=0; i<numRows; i++)
   {
      for(int j=0; j<column; j++)
      {
         cout << temperature[i][j]  << "  ";
      }
      cout << endl;
   }
}

int main()
{
   ifstream inFile;
   inFile.open("grades1.txt");
   if (inFile)
   {
      double temperature[row][column] = {};
      int numRows = readData(inFile, temperature);
      printData(temperature, numRows);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM