C++ Arrays/fstream

Hi, I am creating a simple program for Report Card data entry. My question is that I need to create two set of vectors, since my data is different.

student_name.txt
Name
Age
Gender
Student_ID
Mark #1
Mark #2
Mark #3
Mark #4

The issue is how do I create a separate vector to deal with only marks? For now, my program is storing everything in the same vector, which I don't want because I want to perform the calculation: finding average, mean and etc...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Student::getStudent(string name_file)
{
    string line;
    vector<string> student_info;

    ifstream myFile (name_file.c_str());
    if(myFile.is_open())
    {
        while (getline (myFile, line))
            student_info.push_back(line);
    }
    printf("Name: %s\nAge: %s\nGender: %s\nStudent ID: %s", student_info[0].c_str(), student_info[1].c_str(),
    student_info[2].c_str(), student_info[3].c_str());
}


I am fairly new with vectors, and still doing a bit research a bit.
One student is not worthy of a vector. Define a struct for student-type data that has the necessary data fields.
Last edited on
And use C++ streams instead of <cstdio> functions like printf().

@jlb,
Oh no the printf() is just for testing, I am going to remove once I have complete my method.


@keskiverto,
I would look more into struct; If I got any problem, I post in here again.
Well, I did bit research and found what I was looking for. So here is my solution towards it.

I use a conditional statement to control the amount of lines to be stored in my first vector:
student_info
. Once the condition is done, it goes to another condition which than starts to store my second vector:
student_marks
.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Student::getStudent(string name_file)
{
    string line;
    vector<string> student_info, student_marks;

    ifstream myFile (name_file.c_str());

    if (myFile.is_open())
    {
        for (int lineno = 0; getline(myFile, line) && lineno < 8; lineno++)
            if (lineno < 4)
                student_info.push_back(line);
            else
                student_marks.push_back(line);
    }

    printf("Name: %s\nAge: %s\nGender: %s\nStudent ID: %s", student_info[0].c_str(), student_info[1].c_str(),
    student_info[2].c_str(), student_info[3].c_str());
    cout << endl;
    printf("Mark #1: %%%s\nMark #2: %%%s\nMark #3: %%%s\nMark #4: %%%s", student_marks[0].c_str(), student_marks[1].c_str(),
    student_marks[2].c_str(), student_marks[3].c_str());
}
Last edited on
Topic archived. No new replies allowed.