AYBODY KNOWS WHY THIS PROGRAM IS ABNORMAL?
#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 ("STA83EXMSOL.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 ("401.txt");
if (myfile.is_open())
{
int temp, flag;
for ( int i = 0 ; i < exams.size (); i++ )
{
flag = 0;
for ( int j = 0;j<exams.at(i).total.size(); j++ )
{
myfile<<"\n"<<i+1<<":"; // output student id
myfile<<" "<< exams.at (i).total.at(j)<<"\t" ; // output list of exam codes for this student
myfile<<"\n";
if ( exams.at (i).total.at(j + 1) >exams.at (i).total.at(j) )
{
temp =exams.at (i).total.at(j+1);
exams.at (i).total.at(j + 1) = exams.at (i).total.at(j);
exams.at (i).total.at(j) = temp;
flag = 1;
}myfile << endl << "Double data initially in descending order" << endl;
}
}
}
}
}