How to input file information an assign them to struct values?

I have to take information from a text file and input it and assign the values to values inside a structure. I just learned some basics to structs. How do I get each line of the text file to be assigned to each value of the structure? The other thing that I am stuck on and haven't started yet, is how to count the number of lectures and put the courses into a vector. The sample output is supposed to be more condensed and I put an example at the end.

text file:

FNAME: Mike
MINIT: B
LNAME: Smith
ID: 912345678
USER: abs77

CMPSC121-002-003L
- M 9 05 AM 50 Lecture
- T 10 05 AM 110 Lab
- W 9 05 AM 50 Lecture
MATH140-001
- M 12 25 PM 50 Lecture
- T 12 25 PM 50 Lecture
- W 12 25 PM 50 Lecture
- R 12 25 PM 50 Lecture
OSU007-001
- W 06 00 PM 50 Lecture
ENGL101-004
- M 11 15 AM 50 Lecture
- W 11 15 AM 50 Lecture
- F 11 15 AM 50 Lecture
PHYS211-003-006L
- T 3 35 PM 90 Lecture
- R 3 35 PM 90 Lecture
- F 12 00 PM 110 Lab


#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

struct Course
{
string coursename;
int lectures;
};

struct Student
{
string fname;
char minit;
string lname;
string id;
string username;
vector<Course>enrolled;
};



int main(){
Student professor;
Course types;
ifstream inputFile;

inputFile.open("psuid.txt");

while(!inputFile){
cout << "File not opened!" << endl;
}

getline(inputFile, professor.fname);
getline(inputFile, professor.minit);
getline(inputFile, professor.lname);
getline(inputFile, professor.id);
getline(inputFile, professor.username);


cout << professor.fname << endl;
cout << professor.minit << endl;
cout << professor.lname << endl;
cout << professor.id << endl;
cout << professor.username << endl;
}



Sample output:

FNAME: Mike
MINIT: B
LNAME: Smith
ID: 912345678
USER: abs77

CMPSC121-002-003L
3 lectures

MATH140-001
4 lectures

PSU007-001
1 lecture

ENGL101-004
3 lectures

PHYS211-003-006L
3 lectures
As with most problems, you can solve this one by breaking it down into smaller ones.

To read the student info, start with a function that reads a line with colon-separated fields. I'll call these the keyword and the value. So you want something like:
1
2
// Read a line with format "key : value". Return true on success.
bool readKeyAndVal(string &key, string &val, istream &is);


Write this function, test it with a short main() program.

Next you need methods that will read and write the info for a student This will call readKeyAndVal().
1
2
3
4
5
struct Student {
...
   bool read(istream &is); // read the student info from is
   bool write(ostream &os);  // write the student info to os
};


It would be better to create << and >> operator.s If you don't know those are, then don't worry, just create the read() and write() methods. Again, test these out. Read student info and print it out.

Finally you need to read the lecture info and count the number of lectures. Read the title line for the lecture, then skip whitespace. Now you can use peek() to look at the first character of the next line. If it's a '-' then it's a lecture for the current course. Otherwise it's the start of a new lecture:
1
2
3
4
5
struct Course{
...
    bool read(istream &is);
    bool write(ostream &os);
};

With these building blocks, the code practically writes itself:
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
int main(){
Student professor;
ifstream inputFile;

inputFile.open("psuid.txt");

while(!inputFile){
cout << "File not opened!" << endl;
}

professor.read(inputFile);

Course dummy;
while (dummy.read(inputFile)) {
    professor.courses.push_back(dummy);
}

// Print prof info
cout << professor;

// print course info for each course
for (int i=0; i<processor.courses.size(); ++i) {
    cout << professor.courses[i] << '\n';
}
}

Thanks a lot man this should help a lot.
Topic archived. No new replies allowed.