Reading .txt file to 1D and 2D arrays

I have a .txt file that have data that read like this:

Abraham Jackson, 10, 15, 10, 13, 8, 18,
Joe Harrier, 13, 4, 5, 27, 12, 14,
Thomas High, 21, 2, 4, 15, 7, 3,
Jeffrey John, 4, 9, 8, 5, 27, 12,
Jason Smith, 3, 25, 8, 7, 4, 13,
$



My goal is to take the names and numbers from the file and storing them into a 1D array that contain string and a 2d array that contain integers. I am currently struggling with writing two loops in which one would read and store only the names into the 1D array and one would read and store only the numbers into the 2d array. Another part that I'm also stuck on is incorporating the use of delimiter in order to deal with the commas. Please provide me any examples or tips for writing loops for this type of task. Thank you.

This is what I've wrote so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main()
{
	const int SIZE = 30;
	string names[50];
	int scores[6][5];
	ifstream inputFile;
	inputFile.open("data.txt");
	for (int i = 0; i < 50; ++i)
	{
		inputFile >> names[i];
		cout << names[i] << endl;
	}
	system("PAUSE");
	return 0;
}


The output that I got:

Abraham
Johnson,
10,
15,
10,
13,
8,
18,
Joe
Harrier,
13,
4,
5,
27,
12,
14,
Thomas
High,
21,
2,
4,
15,
7,
3,
Jeffrey
John,
4,
9,
8,
5,
27,
12,
Jason
Smith,
3,
25,
8,
7,
4,
13,
$









Press any key to continue . . .



Last edited on
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
60
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;


struct Person
{
   string name;
   vector<int> data;
};


istream &operator >> ( istream &strm, Person &p )
{
   string notName, temp;
   p.data.clear();
   if ( getline( strm, p.name, ',' ) && getline( strm, notName ) )
   {
      stringstream ss( notName );
      while ( getline( ss, temp, ',' ) ) p.data.push_back( stoi( temp ) );
   }
   return strm;
}


int main()
{
   vector<Person> people;
   Person p;

   // Read data into structs of type person
   ifstream in( "data.txt" );
   while ( in >> p ) people.push_back( p );
   in.close();

   // I suspect that the above is what you really want.
   // However, for a 1-d array of names and 2-d array of ints ...
   vector<string> names;
   vector< vector<int> > numbers;
   for ( Person x : people )
   {
      names.push_back( x.name );
      numbers.push_back( x.data );
   }

   // Write results
   cout << "Names:\n";
   for ( string s : names ) cout << s << '\n';

   cout << "\nData:\n";
   for ( auto row : numbers )
   {
      for ( int i : row ) cout << setw( 3 ) << i;
      cout << '\n';
   }
}


Names:
Abraham Jackson
Joe Harrier
Thomas High
Jeffrey John
Jason Smith

Data:
 10 15 10 13  8 18
 13  4  5 27 12 14
 21  2  4 15  7  3
  4  9  8  5 27 12
  3 25  8  7  4 13



You can also take a look at this thread:
http://www.cplusplus.com/forum/beginner/230228/#msg1042022
She seems to be in the same class!
Last edited on
Thank you but I have never reached the concept of vector yet so is there any method to write it using just arrays only?
I answer this with caution as, without seeing the actual terms of your assignment, I'm not sure that a 2-d array is desirable: it's hard to see why a name should be unconnected to the sequence of numbers after it and the only connection here would be the first index of parallel arrays.

getline() - see http://www.cplusplus.com/reference/string/string/getline/
has both a two-parameter form which will read (the rest of) a line into a string:
getline( stream, string );
and a 3-parameter form to read strings up to a delimiter, in your case a comma ',':
getline( stream, string, delimiter );

If you read your items first as strings then you can convert them to integers with stoi(); see http://www.cplusplus.com/reference/string/stoi/
The alternative is a messy sequence of reads with >> and either ignore() or reading a char to clear the comma.

You can look at how getline() and stoi() are used in my code, even if you don't like the rest of that code.

If you use fixed-size arrays, rather than vectors, then you will have to know in advance how many rows and columns you have (or, at least, the maximum numbers that they might be). You haven't given enough information to see if that is the case.
Last edited on
Topic archived. No new replies allowed.