How to read multiple elements in file to parallel arrays?

I have a txt file that looks like this:
Student ID:97707; Grades: 87.73, 90.41, 91.74, 95.04, 99.13; Name:Davis, Artur
Student ID:23628; Grades: 58.09, 65.18, 68.62, 68.62, 98.05; Name:Davis, Susan
Student ID:49024; Grades: 18.37, 66.06, 68.07, 80.91, 96.47; Name:DeGette, Diana

I need to read the id, grades and names;
The id to a separate array
the grades to a 2-d array
and the names to a c style string.

Where do I start, im having trouble read the dummy to start

Can you help me start?

Thanks!

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
int main()
  {
    char filename[15] = "ex.txt";
    string names[MAX_NAMES];
    double grades[MAX_ROWS][MAX_COLS];
    int id[MAX_IDS];
 
    int index = 0;
    ifstream fin;
    int idnumbers;
    double grade;
    int ids;
    char dummy[1000];
   
 
    fin.open( filename );

    if( !fin.good() )
     {
       cout << "File cannot be found!" << endl;
     }

    fin >> dummy;
    fin >> idnumbers;
    fin >> dummy;
    fin >> grade;
    fin >> dummy;
  
    while( fin.good() )
      {
   
       cout << ids<< " "; //doesn't display anything
       cout << grade << " "; //doesn't display anything
   
       fin >> dummy;
       fin >> idnumbers;
       fin >> dummy;
       fin >> grade;
       fin >> dummy;
      }

   

 

system("pause");
return 0;
}
Are you allowed to use structs? Any time I see the phrase "parallel arrays" that is an instant clue that a data structure is needed.
No I can't use structs
well you have to start by parsing the input strings, : and ; seem to be a good sentinel char to look for

so for example

get the first line of input and store it in a string then
lets say that name is strInput
and then lets declare a couple ints to use as indexes
lets call them s and e

1
2
3
4
5
6
s = strInput.find_first_of(':') + 1;
e = strInput.find_first_of(';');


id[i] = atoi( strInput.substr(s, e - s );
strInput = strInput.substr(e + 1);


and so on...

for the grades, use the a similar parse technique but just us a for loop and that is easy cuz you know each student only has 5 grades
Last edited on
Topic archived. No new replies allowed.