I have this data..but something error..my data is like this...
Exam TExam
1 25
2 20
3 46
4 56
5 12
but it only appear like this
1: -853617
#include <iostream> // std::cout
#include <fstream>
#include <iomanip>
#include <string> // std::string
#include <vector> // std::vector<>
#include <algorithm> //std::for each()
using namespace std; // import "std" namespace into global namespace
struct exam
{
string examid;
vector <int> total;
};
int myfile;
int main()
{
ifstream stream1 ("STA83SOL.txt");
if ( !stream1 )
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "Fail Di buka....." << endl;
}
vector <exam> exams;
exam aExam;
string tempExamID;
bool readEntireFile = false; // set to true when reach end of file
stream1 >> tempExamID; // read in student id of first student
while ( !readEntireFile )
{
aExam.examid = tempExamID; // new student
int tempTotal;
aExam.total.clear ();
stream1 >> tempTotal; // read in first exam code for this student
aExam.total.push_back (tempTotal); // add this exam code to current student's vector of exam codes
bool newExam = false; // true when a new student id is encountered
while ( !newExam && !readEntireFile )
{
if ( stream1 >> tempExamID ) // successfully read in student id
{
if ( tempExamID.compare (aExam.examid) == 0 ) // student id is same as before
{
stream1 >> tempTotal; // read in exam code
aExam.total.push_back (tempTotal); // add this exam code to this student;s vector of exam codes
}
else
newExam = 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
exams.push_back (aExam); // 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.
{
ofstream myfile;
myfile.open ("400.txt");
if (myfile.is_open())
{
myfile<<"+---------------------------------------------------+\n";
myfile<<"| Conflict Graph | |\n";
myfile<<"+---------------------------------------------------+\n";
for ( int i = 0 ; i < exams.size (); i++ )
{
cout<<"\n"<<i+1<<":"; // output student id
for ( int j = 0;j<exams.at(i).total.size(); j++ )
{
cout<<" "<< exams.at (i).total.at(j)<<"\t" ; // output list of exam codes for this student
}
myfile<<"\n\n";
}
}
}
}