#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
usingnamespace 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.
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();
}
}
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.
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.