Restarting/simplifying, data into 2d array

I have this file, that reads something like:
1
2
3
4
tom doe,35, 68, 99,
Jane doe,24, 55, 96,
ben doe, 46, 73, 94,
Sussie doe, 23, 56, 77,


Now I read the values into two strings, one with just the names and one with just the numbers. Then I simply took the name string and put it into a array and then a new string array by concatenating the last names and first names together with a space so that when the calling a index of the new array, it would read "Tom Doe" properly.

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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

int main() {

	const int SIZE = 10;
	const int SCR = 5;
	const int COL = 6;
	const int RAW = 30;
	//int scoresdata[SIZE][SCR];
	
	ifstream inputFile;
	inputFile.open("CPSC121data.txt"); // Open the input file

	int i = 0;

string names, rawScores;
string name1[SIZE];
   ifstream in( "CPSC121data.txt" );
   while ( getline( in, names, ',' ) && getline( in, rawScores )) 
   {
	   
	   //int scoreSingleRow[30] = {0};
string fullnames[SIZE];
stringstream ssin(names);
while (ssin.good() && i < 10){
	ssin >> fullnames[i];
	i++;
}
string finalnames[SCR];
finalnames[0] += fullnames[0] + " " + fullnames[1];
finalnames[1] += fullnames[2] + " " + fullnames[3];
finalnames[2] += fullnames[4] + " " + fullnames[5];
finalnames[3] += fullnames[6] + " " + fullnames[7];
finalnames[4] += fullnames[8] + " " + fullnames[9];

cout << finalnames[3];
   }

//everything above work!!!!!! Names copied into 1D finalnames array.

	cin.get();
	return 0;
}


This is a bit of a cheap workaround I know. The problem is that I think Ive complicated the method too much. If I use what I have now, I will have to covert the string to a int in order to use some math functions on it later. So I'm starting over back to reading in the file. Without using a vector, I believe the best way would be simply using a nested for loop? to read in the names into a 1d array and a parallel 2d array to store the numbers. I know how to set up the for loop for a 1d array but what about nested 2d array?

thanks
Last edited on
Does each line have the same amount of scores? Also, I do suggest using an std::vector, but whether you use an std::vector or a raw array, doesn't change the meat of your code to any significant degree.

Edit: Well, here's one way to do it. A bit messy, I suppose. Could be improved upon. Replace the std::istringstream with your std::ifstream

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

#include <iostream>
#include <string>
#include <sstream>

struct Data { // (You may want to think of a better name)
    std::string name;
    int numbers[3];
};


int main() {
	
    std::istringstream file (
        "tom doe,35, 68, 99,\n"
        "Jane doe,24, 55, 96,\n"
        "ben doe, 46, 73, 94,\n"
        "Sussie doe, 23, 56, 77,\n"
    );
    
    const int Size = 4;
    Data data[Size];
    
    // get data
    for (int i = 0; i < Size; i++)
    {
        std::string name;
        std::getline(file, name, ',');
		
        data[i].name = name;
		
        for (int j = 0; j < 3; j++)
        {
            std::string num_str;
            std::getline(file, num_str, ',');
			
            data[i].numbers[j] = std::stoi(num_str);
        }
	
        // discard extra newline
        std::string temp;
        std::getline(file, temp);
    }
    
    // display data
    for (int i = 0; i < Size; i++)
    {
        std::cout << "name = " << data[i].name << std::endl;
        std::cout << "numbers = ";
        for (int j = 0; j < 3; j++)
        {
            std::cout << data[i].numbers[j] << " ";
        }
        std::cout << "\n" << std::endl;
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.