Reading data from a file into a 2d array

I can't get past this one roadblock in my program. In my program i need to have a for loop function that will read read data from a given data file into arrays (1d array students names and 2d array student scores) however, within the data file, there are commas that serve as delimiters and i must find a way to bypass them.
I was wondering if someone could give me a fix to this?

here is the data file (.txt)

Mary Peterson, 95, 93, 77, 94, 77,
Jake Andersen, 90, 90, 95, 93, 49,
Susan Cooper, 79, 94, 44, 90, 73,
Mike Smith, 95, 93, 30, 79, 97,
Jim Blair, 53, 45, 97, 39, 59,
Clark Lee, 70, 95, 45, 39, 77,
Kennedy Davis, 77, 34, 55, 74, 93,
Kim Bronson, 93, 94, 99, 77, 97,
Sunny Hill, 79, 95, 59, 93, 95,
Sam Benson, 95, 75, 49, 75, 73,
Last edited on
Here is a little example ho to extract the data from a string.
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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>

using namespace std;

const int SCORE_COUNT = 5;

void ExtractData(const string &input, string &name, int score[SCORE_COUNT]);
void ShowData(const string name, int score[SCORE_COUNT]);

int main()
{
  string input = "Mary Peterson, 95, 93, 77, 94, 77,";
  string name;
  int score[SCORE_COUNT];
  
  ExtractData(input, name, score); 
  ShowData(name, score);
  cout << "\n\n";

	system("pause");
	return 0;
}

void ShowData(const string name, int score[5])
{
  cout << "\nName: " << name;
  for (int i = 0; i < SCORE_COUNT; i++)
  {
    cout << "\nScore " << i << ": " << score[i] ;
  }
}

void ExtractData(const string &input, string &name, int score[SCORE_COUNT])
{
  istringstream iss(input);
  char sep;

  getline(iss, name, ',');

  for (int i = 0; i < SCORE_COUNT; i++)
  {
    iss >> score[i];
    iss >> sep; // eat comma
  }

}


All you have to is read the file line by line and call the function ExtractData and store the data in your arrays.
thanks! I'm really new to the language and my professor hasn't gone over the uses of #include <stdlib.h> and #include <sstream>, if you wouldn't mind explaining where and why you used those preprocessors that would be great
Last edited on
#include <stdlib.h> here is only used for system("pause"); so that the console windows doesn't close automatically. If you don't use Visual Studio you might not need it.

#include <sstream> is needed to use istringstream. istringstream is a class used to easily get input from a string.

Hope it's clear, otherwise ask gain.

Have you actually got your data into the arrays?
no not yet, i'm trying to extract the data from the txt file into the arrays.
also another quick question, what did sep do at line 39? i can see you made it to char, and then at line 46 you have it get rid of the comma. i don't really understand how that really worked out.
Last edited on
It's actually quite simple once you understand the principle
string input = "Mary Peterson, 95, 93, 77, 94, 77,";

first you read the name until you find a comma getline(iss, name, ',');
name contains Mary Peterson
then you read the number until the next comma and store it in number. iss >> score[i];
then you have to remove the comma with iss >> sep;
To read the file you basically read the file line by line and extract the data from the line as shown above. Do you know how many lines the file contains ?

thank you so much for explaining that!

oh yeah, the information i gave for the file was in the original post, but i'll post it here again

Mary Peterson, 95, 93, 77, 94, 77,
Jake Andersen, 90, 90, 95, 93, 49,
Susan Cooper, 79, 94, 44, 90, 73,
Mike Smith, 95, 93, 30, 79, 97,
Jim Blair, 53, 45, 97, 39, 59,
Clark Lee, 70, 95, 45, 39, 77,
Kennedy Davis, 77, 34, 55, 74, 93,
Kim Bronson, 93, 94, 99, 77, 97,
Sunny Hill, 79, 95, 59, 93, 95,
Sam Benson, 95, 75, 49, 75, 73,
If you have a fixed number of lines you can define your arrays like this:
1
2
3
4
5
const int NUM_STUDENTS = 10;
const int NUM_SCORES = 5;

string Names[NUM_STUDENTS];
int Socres[NUM_STUDENTS][NUM_SCORES];
Topic archived. No new replies allowed.