inputting a .txt file into a 2 arrays via void function

I need some help with exactly what the title says. I have a .txt file that I need to input into two parallel arrays. The first array needs to be on dimension and the second needs to be two dimensions.

This is a sample from the .txt file:
Australia 62.7 62.1 63.3 59.7
Austria 0 52.8 53.1 54.6
Belgium 30.4 30.3 27.5 25.3
Canada 61.3 56.2 57.7 54.5
Chile 0 26.4 25.4 31.1
CzechRepublic 0 38.3 27.3 25.2
Denmark 65.0 67.1 62.3 55.0
Estonia 51.7 32.9 29.8 34.3
Finland 55.2 42.9 42.1 43.3
France 35.7 28.3 30.2 28.8
Germany 56.4 47.2 42.6 46.6
Greece 30.3 26.9 25.0 13.1
Hungary 0 32.5 21.8 18.6
Iceland 0 68.2 71.6 66.0

this is what I have for code so far:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
const int SIZE = 40;
const int COLUMN = 5;
void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count);

int main()
{
string names[SIZE];
double testData[SIZE][COLUMN];
char grade[SIZE];

ifstream inFile;

inFile.open("C:\\Users\\Dylan\\Desktop\\employed_15-24yrsOld.txt");

if (!inFile)
{
cout << "Cannot open the input file: ch8_Ex13Data.txt." << endl;
cout << "Program terminates!" << endl;
return 1;
}

cout << fixed << showpoint << setprecision(2);

getData(inFile, names, testData, SIZE);


inFile.close();

system("pause");
return 0;
}

void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count)
{
int row, col;

for (row = 0; row < count; row++)
{
for (col = 0; col < COLUMN; col++)
{
inf >> n[row] >> tstData[row][col];
}
}
for (row = 0; row < count; row++)
{
for (col = 0; col < COLUMN; col++)
{
cout << n[row] << " " << tstData[row][col] << " ";
}
}
cout << endl;
system("pause");
}




when I compile and run the code and have it display it does not read the first item into the 1-d array, instead it appears to read the 4th number from the left into the 1-d array and then into the second spot in the 2-d array, then again in its proper place and finally it has this number repeating through the rest of the arrays: -92559631349317830000000000000000000000000000000000000000000.00 followed by the number 59.7 from the .txt and the long number again.


Any help would be greatly appreciated. I have officially hit a point of just not knowing what I need to do. I've consulted my text book and google for about 7ish hours today alone.

When you're reading the file you are reading the "name" too often. There is only one name per line and there are only 4 numeric values per line.

I'm not sure I understand what you are getting at.
Try printing the name right after you read it from the file.

1
2
3
4
5
6
7
8
9
10
...
   for (row = 0; row < count; row++)
   {
      for (col = 0; col < COLUMN; col++)
      {
         inf >> n[row] >> tstData[row][col];
         cout << n[row] << endl;
      }
   }
...


You should see that the name is only correct the first time through the loop. You should be reading the name in the outside loop, not the inside loop. The outside loop is to read each line, the inside loop is to read each of the numeric entries contained in each line.

Since your input file could vary in the number of lines I would recommend something like this to read your file:

1
2
3
4
5
6
7
8
9
10
11
   int line =  0;

   // Read the country name as long as there is room in the array and there is data to read.
   while(line < SIZE && inf >> n[line])
   {
      for (int num_readings = 0; num_readings < COLUMN; num_readings++)
      {
        inf >> tstData[line][num_readings];
      }
      line++;
   }
I tried displaying the output of the array for country names and all it out put was 59.759.759.759.759.7

To me that says it's not even reading in the first country name at all. I will try the loop structure you suggested and see if that makes a difference.

Edit:
Tried that loop structure and it did read in the first name correctly. How ever when I display it from inside the loop it only displays the first country and when I display it from outside the loop it displays nothing. Also the country names are supposed to be parallel with the numeric data and I'm not really sure how to accomplish that.
Last edited on
Show the code where you've tried to implement the loop and print the data.

Have you done much of that yourself? I think I have the same assignment as you but this is what my teacher gave us in the lab file-------

The main driver function is provided:

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
38
39
40
41
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
const int SIZE = 40;
const int COLUMN = 5;
void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count);
void calculateAverage(double tstData[][COLUMN], int count);
void calculateGrade(double tstData[][COLUMN], char gr[], int count);
void print(string n[], double tstData[][COLUMN], char gr[], int count);
  
int main()
{
	string names[SIZE];
    double testData[SIZE][COLUMN];
    char grade[SIZE];

    ifstream inFile;

    inFile.open("employed_15-24yrsOld.txt");

    if (!inFile)
    {
        cout << "Cannot open the input file: ch8_Ex13Data.txt." << endl;
        cout << "Program terminates!" << endl;
        return 1;
    }

    cout << fixed << showpoint << setprecision(2);

    getData(inFile, names, testData, SIZE);
    calculateAverage(testData, SIZE);
    calculateGrade(testData, grade, SIZE);
    print(names, testData, grade, SIZE);

    inFile.close();

	return 0;
}


Not sure if you received the same information but if not this should help with what you have
Yes it's the same assignment but that main function doesn't really help with a ton.
Hey I have messed around with it some so far and I get the output but its just in rows and when I tried a to get it into columns i had issues but here is what the code------

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
const int SIZE = 40;
const int COLUMN = 5;
void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count);

int main()
{
	string names[SIZE];
	double testData[SIZE][COLUMN];
	char grade[SIZE];

	ifstream inFile;

	inFile.open("employed_15-24yrsOld.txt", ios::in);

	if (!inFile)
	{
		cout << "Cannot open the input file: ch8_Ex13Data.txt." << endl;
		cout << "Program terminates!" << endl;
		return 1;
	}

	cout << fixed << showpoint << setprecision(2);

	getData(inFile, names, testData, SIZE);


	inFile.close();

	
	system("pause");
	return 0;
}

void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count)
{
	int row, col;

	for (row = 0; row < count; row++)
	{	


		for (col = 0; col < COLUMN; col++)
		{
			
			inf >> n[row];
			cout << n[row] << endl;
			
		}
	}
	
	cout << endl;
	system("pause");
}


Let me know if you find out anything to change the format without errors, thank you.
Okay lets see if I can make this a little "simpler".
Instead of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count)
{
	int row, col;
	for (row = 0; row < count; row++)
	{	
		for (col = 0; col < COLUMN; col++)
		{
			inf >> n[row];
			cout << n[row] << endl;
		}
	}
	
	cout << endl;
}


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
// Add this include.
#include <sstream> 
...
void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count)
{
   int row = 0, col = 0;
   string line;

   // Read one entire line from the file until there is no more.
   while(getline(inf, line))
   {
      istringstream sin(line); // Create a stringstream to process this line.

       // Get the country name.
      sin >> n;
      cout << name[row] << " ";

      // Read all of the data points.
      while(sin >> tstData[row][col])
      {
         cout << tstData[row][col++] << " ";
         // Insure the array doesn't overflow the array.
         if(col == SIZE)
             break;
      }
      cout << endl;
      row++;
      // Insure you don't overflow the array.
      if(row == SIZE)
         break;
   }
}


Jim
Ty Jim! I was wondering though the way you did it does it allow me to then average each each row?

Thanks again!
Topic archived. No new replies allowed.