using vector in fstream

closed account (1w0XoG1T)
i have a problem using fstream and allocating them into vector

problem statement:
create a program that uses vector called List
using a structure called Students

include the following in the Students
-fullName [string]
-idNumber [int] (e.g 200910001)
-subjectName [string] (e.g. Programming 1, Web Application)
-classStanding [double]
-prelimExam [double]
-midtermExam [double]
-finalExam [double]

now there are 2 operations involved using function inside the structure:

computing cfrs
{
computeCFRS = (classStanding * .40) + (prelimExam *.10) + (midtermExam * .20) + (prefinalExam * .10) + (finalExam *.20)
}

getting the letter grade
{
Letter Grade Cfrs
A 95 above
B+ 91 - 95
B 81 - 90
C 71 - 80
D 61 - 70
F 60 below


}


thats all for the structure

heres the tricky part
i know fstream uses input and output from a file operation in C++

the input file contains:
exmaple:

Adrian S. Andalis
200810925
College Algebra
60 90 40 85 60

Micheal R. Solis
200712634
Discrete Structures
85 75 75 78 73

Christian S. David
200810159
Web Application
60 -10 50 40 0


the ouput file contains:
from data input above:

Adrian S. Andalis
200810925
CFRS: 55.5
Letter Grade: F

Micheal R. Solis
200712634
CFRS: 79.6
Letter Grade: C

Christian S. David
200810159
Alert: Grades are invalid for this
Particular Student

i really don't get much help here, using fstream and input/output/manipulate them is easy, but using vector? can anybody help me?
You need a function to read the data into a Student, and you need a function to write a Student to file.

I recommend overloading the stream extraction and insertion operators:
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
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

class Student
  {
  public:
    string fullName;
    ...
  };

istream& operator >> ( istream& ins, Student& student )
  {
  ins >> ws;
  getline( ins, student.fullName );
  ...
  return ins;
  }

ostream& operator << ( ostream& outs, const Student& student )
  {
  outs << student.fullName << endl;
  ...
  outs << endl;
  return outs;
  }

Now you just need to read a Student from file while the file is good():
1
2
3
4
5
ifstream infile( "fooey.txt" );
vector <Student> students;
Student student;
while (infile >> s)
  students.push_back( student );

Use similar code to output the students.

Hope this helps.
Topic archived. No new replies allowed.