Reading Data File into a 2D Array

Hey everyone, I'm new to this site but I'm fed up with an assignment and I need some assistance.

I need to read a data file into a 2 dimensional array, and I believe that is the hardest part of the assignment. Here's my code:

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
using namespace std;

// Global Variables
int FactoryData[6][4]; // ?

int main()
{
// Read Data File into a 2 Dimensional Array
ifstream inFile ("FactoryData.txt", ios::in);
inFile.precision(2);
inFile.setf(ios::fixed, ios::showpoint);

inFile >> // ?
while (!inFile.eof())
{
for (int factory = 0; factory < 6; factory++)
{
for (int shift = 0; shift < 4; shift++)
{
FactoryData[factory][shift] = // ?
}
}
}
inFile.close();
}

This is not my entire code; it's only the part I'm having trouble with
a "?" means that I have no idea what to put there.

Here's the data from the "FactoryData.txt" file:

300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290

6 rows, 4 columns

Thank you!
Last edited on
Just use inFile >> FactoryData[factory][shift]
A two dimensional array simply stores one value into a position, its not much different from a 'normal' array.

Your initial declaration of int FactoryData[6][4]; is fine as it is, but if you already had the values on hand you could have declared it like this:

1
2
int FactoryData[][] = {300 /*[0][0]*/, 450 /*[0][1]*/, 500, /*[0][2]*/, 210 /*[0][3]*/}; //The [0][x] block
                      {510 /*[1][0]*/ // And so on. 


For the inFile part use what Bazzy did and your last part will need to be taking the value you get out of inFile and putting it into that index
Topic archived. No new replies allowed.