Need to read from a txt file into structure, very confused.

Ok so I need to read a bunch of information from a text file and put it into a list of variables in a structure.

I have a list of 6 students, each with their own grades and ID numbers and names in a txt file.

I just started and so far I have 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
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

struct student
{
     char student_name;
     int student_ID;
     int program_grades[15];
     int quiz_grades[12];
     int exam_grades[3];
     int final_exam;
     float prav;
     float quav;
     float exav;
     float final_score;
     char final_grade;
};

int main()
{
    int student_number;
    ifstream fin;
    fin.open("studentdata.txt");
    fin >> student_number;
    fin >> name1;
    cout << "Ther are " << student_number << " students in this class.\n";
    
    student bill =
    {
            
    }
    
    system ("pause");
}


So my question is how can I get all the info from the txt file into that structure (each variable).

I have to do this for 6 students, how can I do this for all of them?
It would depend on how you file is formatted. If you know the format, just use that and read it into the correct variable. Also, I think your student_name variable should be an std::string, not a char.
Ok thanks, I am just not sure of the syntax to read in.

I tried doing fin.student_name; but it just tells me that the member doesn't exist in structure I am not sure how to do it.

This assignment is too hard!!!!! I have only one day, no!!!!!
Last edited on
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
student students[6];
getStudents (students, 6);
void getStudents (students s[], int size)
{
    ifstream fin; //declaring an in filestream called fin
    fin.open ("students.txt");  //opening file named students.txt
    if (!fin.fail())
    {
        for (int i = 0; i < size; i++) //loop your array of students
        {
            Use Getline to get 
            all of your attributes
            
           ex. getline(fin, students[i].student_name);
      
            char student_name;
     int student_ID;
     int program_grades[15];
     int quiz_grades[12];
     int exam_grades[3];
     int final_exam;
     float prav;
     float quav;
     float exav;
     float final_score;
     char final_grade;
        }
        fin.close();
    }
}


You must also save it in the same order.
Last edited on
Ok so I will do a getline for each one? Thanks I am gonna give this a shot.

Also for the [15] [12] and [3] will it input numbers till they arrays are full? That's what I need it to do.
Last edited on
yes.. but where is your file inc from?? a major part of this is the file that you open and how the text is formatted in it.

here is part of my journal program my file input and output. use as an example.
i included my structs so you can see what they are.


1
2
3
4
5
6
7
8
9
10
11
12
13
struct entry
{
    string day;
    string event;
};
struct month
{
        int year;
        string months;
        int firstDayinmo;
        vector<entry> thismonth;
        int dinmo;
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void save (month a[], int size)
{
    ofstream fout("Month.txt");

    if (!fout.fail())
    {
        for (int i = 0; i < size; i++)
        {
            fout << a[i].year << endl;
            fout << a[i].months << endl;
            fout << a[i].dinmo << endl;
            fout << a[i].firstDayinmo << endl;

            for (int j = 0; j < a[i].dinmo; j++)
            {
                fout << a[i].thismonth[j].day << endl;
                fout << a[i].thismonth[j].event << endl;

            }
        }
        fout.close();
    }
}


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
void getJournal (month a[], int size)
{
    ifstream fin;
    fin.open ("Month.txt");
    if (!fin.fail())
    {
        for (int i = 0; i < size; i++)
        {
            string yearString, dinmoString, firstDaystring;

            getline(fin, yearString);
            getline(fin, a[i].months);
            getline(fin, dinmoString);
            getline(fin, firstDaystring);
            a[i].year = atoi(yearString.c_str());
            a[i].dinmo = atoi(dinmoString.c_str());
            a[i].firstDayinmo = atoi(firstDaystring.c_str());

            for (int j = 0; j < a[i].dinmo; j++)
            {
                entry e;
                getline(fin,e.day);
                getline(fin,e.event);
                a[i].thismonth.push_back(e);
            }
        }
        fin.close();
    }
}
Last edited on
Ok thanks a bunch, the file I am reading from is a text file.

The information inside it will look like this, the first number is an ID the next 15 are program grades, next 12 are quiz grades, then 3 exam grades and 1 final grade.

The floats in the structure will be averages, program average, quiz average, and exam averages.
Dangg this is more complicated than I thought.

Bowles,Bill
123456
6
2
5
5
5
3
5
6
0
4
6
6
2
5
5
19
18
19
12
17
15
19
18
19
12
17
15
87
85
91
120
read them in one as a time through that loop a getline for each. you will have to put a few for loops inside the first loop of students just so you can fill the different grade values.

getline for name
getline for id

for loop j <15
getline programgrades

for loop k <12
getline quiz

for loop l<3
getline exam

getline final

example in your for loop for quiz you will have

getline (fin, students[i].quiz_grades[k])
Last edited on
Topic archived. No new replies allowed.