I have been assigned to write a program that reads data from a file and loads it into a 2D array (6 rows, 4 columns). I have done that, but i have noticed something:
what displays in the main:
300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290
300
what displays in the "display" function:
300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290
I would just like to know why the main displays an extra element? the for loop in the main will not be included in the final product, i was just curious to see what would happen. If both functions use the same for loop, shouldn't the output be the same? Here is my code. I would like some feedback on my code as well, if you have the time. The program isn't finished, I'm taking it one capability at a time.
//assignment 9: load a datafile that contains 6 factories' shifts and production runs
// and load it into a 2D array. Then, the user chooses to display the info to
// find the averages, highest, and lowest shift productions of the factories AND
// the total average for all 6, and to pick a location in the datafile to manipulate
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdlib>
usingnamespace std;
constint row = 6, cols = 4;
void display(int array_copy[row][cols])
{
for (int r = 0; r < row; r++)
{
for (int c = 0; c < cols; c++)
{
cout << array_copy[r][c] << "\t";
}
cout << "\n";
}
}
int main()
{
int factories[row][cols] = { {0} };
ifstream inFile ("FactoryData.txt", ios::in);
//if the file doesnt exist, display an error message.
if (!inFile)
{
cout << "File not found.\n";
//break;
}
//this for loop will dump the contents of the data file into the array
for (int r = 0; r < row; r++)
{
for (int c = 0; c < cols; c++)
{
inFile >> factories[r][c];
cout << factories[r][c] << "\t";
}
cout << "\n";
}
cout << factories[0][0];
int choice = 0;
cout << "\n1) Display Factory Shifts & Production\n";
cout << "Enter an option: ";
cin >> choice;
switch (choice)
{
case 1:
{
display(factories);
break;
}
}
}
for (int r = 0; r < row; r++)
{
for (int c = 0; c < cols; c++)
{
inFile >> factories[r][c];
cout << factories[r][c] << "\t";
}
cout << "\n";
}
cout << factories[0][0]; // you print the position [0][0] which contains 300, the duplicated print