How to cal the examid like i want to call 001 and count how many exam 001 clash with 002, 003, 004...?
// Filename : ConflictMatrix.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct student
{
string studentid;
vector <int> examcode;
};
int main()
{
ifstream stream1 ("STA83STU.txt");
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
vector <student> students;
student aStudent;
string tempStudentID;
bool readEntireFile = false; // set to true when reach end of file
stream1 >> tempStudentID; // read in student id of first student
while (!readEntireFile)
{
aStudent.studentid = tempStudentID; // new student
int tempExamCode;
aStudent.examcode.clear ();
stream1 >> tempExamCode; // read in first exam code for this student
aStudent.examcode.push_back (tempExamCode); // add this exam code to current student's vector of exam codes
bool newStudent = false; // true when a new student id is encountered
while (!newStudent && !readEntireFile)
{
if (stream1 >> tempStudentID) // successfully read in student id
{
if (tempStudentID.compare (aStudent.studentid) == 0) // student id is same as before
{
stream1 >> tempExamCode; // read in exam code
aStudent.examcode.push_back (tempExamCode); // add this exam code to this student;s vector of exam codes
}
else
newStudent = true; // student id is different from before. Therefore new student.
}
else
readEntireFile = true; // end of file reached. Want to exit inner and outer while loops
} // if new student, do not repeat this while loop
students.push_back (aStudent); // no more exam codes for this student. Add aStudent to students vector
}
stream1.close (); // We have read the entire file, so time to close it.
for (int i = 0; i < students.size (); i++)
{
cout << students.at (i).studentid << endl; // output student id
for (int j = 0; j < students.at (i).examcode.size (); j++)
cout << students.at (i).examcode.at (j) << "\t"; // output list of exam codes for this student
cout <<"\n"<<endl;
}
return 0;
}