Reading a table from a .txt file and printing a 2D array

This is a homework question, but the answer I seek is not for the entire homework; this is only the first part of it. The rest is simple calculations that I need to do, alone with switch/case scenarios. I am only asking this part of the question as I am admittedly bad at arrays.

For my class assignment, we are being requested to take data from a .txt file and produce some data into 2D arrays with it. The only question (issue) I have is that I am trying to read this file into a 2D array in the first place.
The table is supposed to be 34 rows long, 14 columns wide; the first column is dedicated to years. I'll feature an example of such, but I wish to try to get this myself, so I will refrain from adding the whole thing:

1982 1192004.204 146797.49 305259.749 0 282773.248 0 312374.013 195.94
124.979 4842.865 0 0 2244372.487
1983 1259424.279 144498.593 274098.458 0 293677.119 0 335290.855 215.867
162.745 6075.101 0 2.668 2313445.686
1984 1341680.752 119807.913 297393.596 0 327633.549 0 324311.365 461.411
424.54 7740.504 5.248 6.49 2419465.367

This is the code I am putting up as I think it should be.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

ifstream infile;

int main()
{
    //open data files
    infile.open("Energy.dat");

    double energy[34][14];

    //Assigning data to arrays row by row
    for(int i = 0; i < 34; i++)
    {
        for(int j = 1; j <= 14; j++)
        {
            infile >> energy[i][j];
            cout << energy[i][j] << "\t";
        }
        cout << "\n";
    }
    return 0;
}


My result comes out with mainly arbitrary numbers, even the first one. On top of that, the code output doesn't come back as a table of 34 x 14. Any help would be greatly appreciated!
Last edited on
using std::vector<std::vector<double>> as the 2d container:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>

constexpr auto SIZE = 14;

int main()
{
    std::ifstream inFile {"D:\\test.txt"};
    std::vector<std::vector<double>> myVec{};
    while(inFile)
    {
        size_t i{};
        std::vector<double> tempVec{};
        while (i < SIZE)
        {
            double data{};
            inFile >> data;
            tempVec.push_back(data);
            ++i;
        }
        if(inFile)
        {
            myVec.push_back(std::move(tempVec));
        }
        tempVec.clear();
    }
    for (const auto& elem : myVec)
    {
        for (const auto& elemI : elem)
        {
            std::cout << std::setprecision(10) << elemI << " ";
        }
        std::cout << "\n";
    }
}
@daft25

Array indices in C++ start at 0. So you will be writing beyond array bounds with your j loop. Change your line 19 to start at 0 and be strictly less than 14; i.e.
for(int j = 0; j < 14; j++)


Actually, it is not good practice to make repeated use of "magic numbers" like 34 and 14 - you might subsequently want to change them and then you would have to be very careful to do so in many places. That is a recipe for error. Better to declare then (as const) with formal names; e.g.
1
2
3
4
5
6
7
8
   const int IMAX = 34;
   const int JMAX = 14;
//
    double energy[IMAX][JMAX];
//
    for(int i = 0; i < IMAX; i++)
    {
       for(int j = 0; j < JMAX; j++)

Then, if you want to change them, you only need to do so in one fairly obvious place.
Last edited on
Topic archived. No new replies allowed.