Dear all,
I have one problem,..I want to count how many examcode and fill in the blank in the matrix below that I have done.
Example is ,, for examcode 1 how many it appear. and for examcode 2 and examcode 1, how many student take both of them??
/*---------------------------
Filename : ConflictMatrix.cpp
---------------------------*/
#include <iostream>
#include <fstream>
#include <iomanip>
#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;
}
/*----------
Count ExamID
----------*/
cout<<"i,j|";
for (int colHead = 1; colHead < 11; colHead++)
cout << setw(3.5) << colHead;
cout << endl;
cout <<"__________________________________"<<endl;
for (int rowVal = 1; rowVal < 11; rowVal++)
{
cout << setw(3.5)<< rowVal <<'|';
for (int colVal =1; colVal < 11; colVal++)
cout << setw(3.5)<<"";
cout <<endl;
}
return 0;
}