Need some help in reading from a file...

Hi guys , I have a problem in reading a name and followed by scores from a file .

my file is student.txt
78541/14 Jon Adam 85.0 78.5 89.0
12345/11 Jesson Bourne 67.5 76.0 82.5
65421/12 Yagami Light 75.5 87.0 79.5

And I should read the file and display it on screen.
Also I must store first Name and Last Name into an array called NAMES.

int size=0;
char NAMES[50][40];
char IDS[50][9];
double SCORES[50][3];

while (inFile.good())
{
inFile >> IDS[size];

inFile.getline(NAMES[size], 40, '\n');
inFile >> SCORES[size][0] >> SCORES[size][1] >> SCORES[size][2];
size++;
}
for (int i = 0; i < size; i++)
cout << IDS[i] << " " << NAMES[i] << " " << SCORES[i][0] << " " << SCORES[i][1] << " " << SCORES[i][2] << endl;

And when I display them I get a mess and I know the reason , because getline reads until the end of 40 characters , but I don't know how to deal with it -_-
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
# include <iostream>
# include <fstream>
# include <sstream>
# include <string>
# include <vector>

struct Student
{
    std::string m_ID;
    std::string m_firstName;
    std::string m_lastName;
    double m_score1;
    double m_score2;
    double m_score3;

    Student(const std::string& ID, const std::string& firstName, const std::string& lastName,
            const double score1, const double score2, const double score3):
            m_ID(ID), m_firstName(firstName), m_lastName(lastName),
            m_score1(score1), m_score2(score2), m_score3(score3){}
};

std::ostream& operator << (std::ostream& os, const Student & s)
{
    os << s.m_ID << " " << s.m_firstName << " " << s.m_lastName <<
        " " << s.m_score1 << " " << s.m_score2 << " " << s.m_score3 << "\n";
    return os;
}

int  main()
{
  std::ifstream inFile{"F:\\test.txt"};
  std::vector<Student> students{};
  if(inFile)
  {
      std::string line;
      while (getline(inFile, line))
      {
          std::istringstream stream{line};
          std::string ID, firstName, lastName;
          double score1{}, score2{}, score3{};
          if(stream)
          {
              stream >> ID >> firstName >> lastName >> score1 >> score2 >> score3;
              if(inFile)
              {
                  students.emplace_back(Student(ID, firstName, lastName, score1, score2, score3));
              }
          }
      }
  }
  for (const auto& elem : students)std::cout << elem ;
}
Thanks for reply , but in my course we did not reach structure topic , only functions , arrays ,cstring .
in my course we did not reach structure topic , only functions , arrays ,cstring .


Well, how about this:
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
    const int maxSize = 50;

    char NAMES[maxSize][40];
    char IDS[maxSize][9];
    double SCORES[maxSize][3];
    
    int size = 0;
    char first[40];
    char last[40]; 
    
    while  (size < maxSize &&
        inFile >> IDS[size] 
               >> first >> last 
               >> SCORES[size][0] 
               >> SCORES[size][1] 
               >> SCORES[size][2] )
    {
        strcpy(NAMES[size], first);
        strcat(NAMES[size], " ");
        strcat(NAMES[size], last);
        
        size++;
    }
    
    for (int i = 0; i < size; i++)
        cout << IDS[i] << " " << NAMES[i] << " " << SCORES[i][0] << " " << SCORES[i][1] << " " << SCORES[i][2] << endl;


Or to become a bit more familiar with functions, remove some of the complexity from main() and put it in its own separate function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool read(istream & in, char * NAMES, char * IDS, double * SCORES)
{
    char first[40];
    char last[40]; 
    
    in >> IDS >> first >> last >> SCORES[0] >> SCORES[1] >> SCORES[2];
    
    if (in)
    {
        strcpy(NAMES, first);
        strcat(NAMES, " ");
        strcat(NAMES, last);
        return true;
    }
    
    return false;
}

and then the code in main() becomes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    const int maxSize = 50;

    char NAMES[maxSize][40];
    char IDS[maxSize][9];
    double SCORES[maxSize][3];
    
    int size = 0;
    
    while  (size < maxSize && read(inFile, NAMES[size], IDS[size], SCORES[size]) )
    {        
        size++;
    }
    
    for (int i = 0; i < size; i++)
        cout << IDS[i] << " " << NAMES[i] << " " << SCORES[i][0] << " " << SCORES[i][1] << " " << SCORES[i][2] << endl;

Topic archived. No new replies allowed.