text file to arrays

so iv'e been going in circles for a while now and i just gave up lol so it goes something like this.
there is this txt file on the inside it has:

student1
grade1
grade2
grade3

student2 .. and so on
so we have to read that info into an array ..the names go into a 1Darray and the grades go on 2Darrays
also the end result must go in a parallel array but thats not so hard


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
const int MAX_SIZE =14;
const int ROW =2;
const int COL =5;
const int ROW2 =4;
const int COL2 =5;
int main(int argc, char *argv[])
{
    char student[MAX_SIZE];
    char student1[2][5][7];

    char student2[MAX_SIZE][MAX_SIZE]; 
   char  grade[MAX_SIZE];
    
    
    ifstream myFile;
   // int cols, rows;
    
    myFile.open("coca.txt");
    if (!myFile)
    {
         cerr << "File does not exist. \nExiting..."<<endl;
         system ("PAUSE");
         exit (EXIT_FAILURE);
    
    
    }
    
    
    for(int r =0; r <=ROW; r++)
    {
     for(int c =0; c <=COL; c++)
     { 
     myFile >> student1[r][c][MAX_SIZE];
     cout << student1[r][c][MAX_SIZE];         
     }        
    }
    
    
       
    
    
    
    
    //string //name1, grade1, grade2, grade3, grade4;
    
   // myFile.getline(student,15, ',');
    
    //cout << student;
    
  //  myFile.getline(grade, 15, ',');
   // cout << grade <<endl;
    
      /*myFile >> student1[0][1];
      cout << student1[0][1];
      myFile >> student2[2][1];
      cout << student2[2][1];
       myFile >> student2[3][1];
      cout << student2[3][1];
       myFile >> student2[4][1];
      cout << student2[4][1];
       myFile >> student2[5][1];
      cout << " " << student2[5][1];
 */


 
 
 /*   
    myFile >>grade2;
    cout << grade2 <<endl;
    
    myFile >>grade3;
    cout << grade3 <<endl;
    
    myFile >>grade4;
    cout << grade4  <<endl;
    
    
    */
    system("PAUSE");
    return EXIT_SUCCESS;
}


to make it short the final output must be something like
john 80 90 40
bush 78 98 78
kat 99 92 100
and i get everything in a row like jhon809040b

thank you guys
tried some things and played around with it for a while .. a lot of google lol but i cant seem to be bale to do it
Replace:
1
2
3
4
5
6
7
8
9
   for(int r =0; r <=ROW; r++)
    {
     for(int c =0; c <=COL; c++)
     { 
     myFile >> student1[r][c][MAX_SIZE];
     cout << student1[r][c][MAX_SIZE];         
     }        
    }
    

With:
1
2
3
4
5
6
7
8
9
for(int r =0; r <=ROW; r++)
{
    for(int c =0; c <=COL; c++)
    { 
        myFile >> student1[r][c][MAX_SIZE];
        cout << student1[r][c][MAX_SIZE] << ' ';         
    }
    cout << endl;
}
Oh, one serious problem that you do have is you declare:
char student1[2][5][7];

But then you write:
myFile >> student1[r][c][MAX_SIZE];
MAX_SIZE = 14 so you have:
myFile >> student1[r][c][14];

You're writing to memory which is out of bounds. That means it could contain ANYTHING and could be over-written by anyone at any time. It is highly likely you will get a segmentation fault next time you run the program. I'd suggest making this a 2d array instead of a 3d array. Even better, I'd use vectors and structures.
thank you guys for your answers
i would use struct's and vectors but for this assignment i cant and i lack so much experience when it comes to working with files any way

i got ridoff the 3d arrays i wasn't even meant to use them :)

now one of the problems is ..is there a way to tell apart a char and an int inside a file?

because as far i as i can tell u can just read everything inside the file with char arrays? its 2:40 in the morning guys ima take a nap but thank you for the response in advance ill try to be up early
Topic archived. No new replies allowed.