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";

}
}
}
}

First thing I'd try is eliminating line 15 as the int named myfile declared there may conflict with line 60 where you declare an ofstream by the name of myfile as well.

If that doesn't work, and it may well not, then I'd try reposting, enclosing all your code, including the last 4 closing curly braces and hope that will maintain indenting to make the code more readable

i HAVE DELETE BUT CANT..

1: -858993460 Press any key to continue

If that doesn't work, and it may well not, then I'd try reposting, enclosing all your code, including the last 4 closing curly braces and hope that will maintain indenting to make the code more readable

So please repost you complete code

#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.is_open()) {
		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 (!stream1.eof() ){
		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 (size_t i = 0; i < exams.size(); i++) {
				myfile<<"\n"<<i+1<<":"; // output student id

				for (size_t j = 0; j<exams.at(i).total.size(); j++) {
					myfile<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
				}
				myfile<<"\n\n";

			}
		}
	}
	return 0;
}

line 15: delete it because its not needed.

Your logic in that while statement beginning on lines 26 to 50 is all screwed up. Here's the correction which is a lot less code than what you have.

int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal);
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
            exams.push_back(aExam);
            aExam.total.clear();
            aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); 
    }
    exams.push_back(aExam);
    stream1.close(); // We have read the entire file, so time to close it.

Dear Ancient Dragon,

I have done what you told before...but why the output is like this..
Fail Di buka.....

1: -858993460 Press any key to continue

the data is like this

1 25
2 30
3 56
4 10

but if the data i arrannge like this...
0001 25
0002 30
0003 56
0004 10

it can produce the output that i want like this..

1 : 25
2 : 30
3 : 56
4 : 10

I don't get that output. Here is the full code of what I used to test

#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.is_open()) {
		cout << "While opening a file an error is encountered" << endl;
	} else {
		cout << "Fail Di buka....." << endl;
	}
	vector <exam> exams;
	exam aExam;
    string tempExamID;
    int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
            exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
            aExam.total.clear();
            aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    }
    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 (size_t i = 0; i < exams.size(); i++) {
				myfile<<"\n"<<i+1<<":"; // output student id

				for (size_t j = 0; j<exams.at(i).total.size(); j++) {
					myfile<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
				}
				myfile<<"\n\n";

			}
		}
	}
    cin.get();
	return 0;
}

Here's the input file I used, which you posted above

1 25 
2 20 
3 46 
4 56 
5 12

output file is this:

+---------------------------------------------------+
|                       Conflict Graph              |                                    |
+---------------------------------------------------+

1: 25	


2: 20	


3: 46	


4: 56	


5: 12

Oh i know,..i dont put the code below..thats why i dont get the output that I want? Actually what is cin.get();?

THANK YOU ancient dragon !

cin.get();
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char **argv) {
	ofstream res("400.txt");
	if (!res.is_open()) {
		cout << "Unable to open file 400.txt";
		return 1;
	}
	ifstream stream1("STA83SOL.txt");
	if (stream1.is_open()) {
		int Exam, TExam;
		while (stream1 >> Exam >> TExam) {
			res << Exam << " : " << TExam << endl;
		}
	} else {
		cout << "Unable to open file STA83SOL.txt" << endl;
		return 2;
	}
	return 0;
}

input file [STA83SOL.txt]
0001 25
0002 30
0003 56
0004 10
output file [400.txt]
1 : 25
2 : 30
3 : 56
4 : 10
is this what you want?

Oh i know,..i dont put the code below..thats why i dont get the output that I want? Actually what is cin.get();?

THANK YOU ancient dragon !

cin.get();

see fstream reference

and just 4 fun

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

vector <pair<int, vector<int> > > myvec;

int exist(int x) {
	for (size_t i = 0; i < myvec.size(); ++i) {
		if (myvec[i].first == x)
			return i;
	}
	return -1;
}

int main(int argc, char **argv) {
	ofstream res("400.txt");
	if (!res.is_open()) {
		cout << "Unable to open file 400.txt";
		return 1;
	}
	ifstream stream1("STA83SOL.txt");
	if (stream1.is_open()) {
		int Exam, TExam;
		while (stream1 >> Exam >> TExam) {
			if (exist(Exam) == -1) {
				myvec.push_back(pair<int, vector<int> >(Exam, vector<int>()));
			}
			myvec[exist(Exam)].second.push_back(TExam);
		}
	} else {
		cout << "Unable to open file STA83SOL.txt" << endl;
		return 2;
	}
	for (size_t i = 0; i < myvec.size(); ++i) {
		res << myvec[i].first << " : ";
		for (size_t j = 0; j < myvec[i].second.size(); ++j) {
			res << myvec[i].second[j] << ' ';
		}
		res << endl;
	}
	cout << "now you can open 400.txt" << endl;
	return 0;
}

input [STA83SOL.txt]
0001 25
0002 30
0003 56
0004 10
0004 30
1 50
2 66
2 77
004 009
output [400.txt]
1 : 25 50
2 : 30 66 77
3 : 56
4 : 10 30 9

Thank you ivailosp!!

I try to do like this..but it repeat repeat and repeat..plz somebody help me......

#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 main() 
	{
	ifstream stream1("STA83SOLUTION.txt");
		if ( !stream1.is_open())
		{
		cout << "While opening a file an error is encountered" << endl;
		} 
			else 
			{
			cout << "Fail Di buka....." << endl;
			}
	vector <exam> exams;
	exam aExam;
    string tempExamID;
    int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
        aExam.total.clear();
        aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    }
    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("411.txt");
		int temp;
			if (myfile.is_open())
			{
				for (size_t i = 0; i < exams.size(); i++) 
				for (size_t j = 0; j<exams.at(i).total.size(); j++) 
				{
				myfile<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
			
					for (size_t i = 0; i < exams.size(); i++) 
					{
					for (size_t j = 0; j<exams.at(i).total.size(); j++) 
					{
						if (exams.at(i).total.size()+1<exams.at(i).total.size())
						{
						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;
						}
						myfile<<exams.at (i).total.at(j)<<"\n";
					}
				
				}
				}
			}
cin.get();
return 0;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.