When doing the File IO_struct program, did you notice that the design of the data structure can be improved? The problem is that some data are redundant in the struct/array structure. For example, the word “Amy” appears three times in the data structure. In real industry systems, no redundant data is allowed, since data in this kind of systems cannot be easily updated, deleted, added or saved. It is extremely error-prone. Thus in practice this type of designs should be avoided.
Now we can redesign the data structure to avoid this severe design problem. We can use either array or vector to do the redesign. After the data structure is loaded with the data, this is what it should look like.
|
||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
|
The 11 rows of data belong to 5 students. Thus, after the data are loaded, there should be exactly 5 student records, not 11 records, in the data structure. And each student record can hold multiple course records.
Ideally the system should allow any number of students to be added and each student should be allowed to take any number of courses. (Do you think that vector is the better choice? Why?)
In this assignment we will redo the File IO/struct/array program by using vector, instead of array. You should feel that the coding is easier and the program is shorter than the array version.
=========================================================
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct Course
{
string course;
int credit;
int score;
};
struct Student
{
int id;
string name;
vector <Course> vec; // means you can have any courses.
};
int main()
{
vector <Student> Students;
fstream inputFile;
string fileName = "StudentRecords.txt";
inputFile.open(fileName.c_str(), ios::in);
if (inputFile.is_open())
{
while(!inputFile.eof())
{
while(!inputFile.eof())
{
}
}
inputFile.close();
}
else
cout << "File cannot be opened.";
return 0;
}
=====================================================
12546 Amy CS1 4 81
13455 Bill CS1 4 76
14328 Jim CS1 4 64
14388 Henry CS3 3 80
15667 Peter CS3 3 45
12546 Amy CS2 4 90
13455 Bill CS2 4 85
14328 Jim CS2 4 71
12546 Amy CS3 3 90
13455 Bill CS3 3 75
14328 Jim CS3 3 69
***StudentRecords.txt***
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct Course
{
string course;
int credit;
int score;
};
struct Student
{
int id;
string name;
vector <Course> vec; // means you can have any courses.
};
int main() {
vector <Student> Students;
fstream inputFile;
string fileName = "StudentRecords.txt";
inputFile.open(fileName.c_str(), ios::in);
if (inputFile.is_open())
{
while(!inputFile.eof())
{
Student s;
Course c;
bool found = false;
// read the id and name of student
inputFile>>s.id>>s.name;
// read the course name, credit and score
inputFile>>c.course>>c.credit>>c.score;
// loop to check if the student with id already exists in vector
for(unsigned int i=0;i<Students.size();i++)
{
// if student exist in vector, add the course to its vector of courses
if(Students[i].id == s.id)
{
Students[i].vec.push_back(c);
found = true;
break;
}
}
// if student doesn't exist in the vector, add the student
if(!found)
{
s.vec.push_back(c); // push the course to the students vector of courses
Students.push_back(s); // push the student in vector of students
}
}
// output the number of students in vector
cout<<"Number of students : "<<Students.size()<<endl;
// loop to output the student and courses details
for(unsigned int i=0;i<Students.size();i++)
{
cout<<"Id : "<<Students[i].id<<" Name : "<<Students[i].name<<endl;
for(unsigned int j=0;j<Students[i].vec.size();j++)
cout<<"\tCourse : "<<Students[i].vec[j].course<<" Credit : "<<Students[i].vec[j].credit<<" Score : "<<Students[i].vec[j].score<<endl;
}
inputFile.close(); //close the input file
}else
cout << "File cannot be opened.";
return 0;
}
//end of program
Output:
Input file:
Output:
Get Answers For Free
Most questions answered within 1 hours.