I am trying to sort a record according to their respective number like this:
5 reyes d r 1 2 3
3 delos d k 4 5 6
9 go t r 7 4 5
1 po w w 2 2 2
2 bun b m 3 3 3
then it will become:
1 po w w 2 2 2
2 bun b m 3 3 3
3 delos d k 4 5 6
5 reyes d r 1 2 3
9 go t r 7 4 5
here is the definition of the code:
void sortRec()
{
fstream data,temp;
string line;
int size(0);
int id[500];
data.open("employee.txt",ios_base::in);
//counting lines
data.seekg(0,ios::beg);
while(getline(data,line))
{size++;}
data.clear();
data.seekg(0,ios::beg);
for(int i=0;i<size;i++)
{
data>>id[i]>>e.name.lname>>e.name.fname>>e.name.mname>>e.address>>e.hour>>e.hRate;
}
//sorting employee numbers
for(int a=0;a<size;a++)
{
for(int b=a+1;b<size;b++)
{
if(id[b]<id[a])
{
int tmp;
tmp=id[a];
id[a]=id[b];
id[b]=tmp;
}
}
}
data.close();
//sorting the data
rename("employee.txt","tempfile.txt");
data.open("employee.txt",ios_base::app);
temp.open("tempfile.txt",ios_base::in);
int _id;
for(int i=0;i<size;i++)
{
temp>>_id;
temp>>e.name.lname>>e.name.fname>>e.name.mname>>e.address>>e.hour>>e.hRate;
for(int a=0;a<size;a++)
{
if(id[a]==_id)
{data<<_id<<" "<<e.name.lname<<" "<<e.name.fname<<" "<<e.name.mname<<" "<<e.address<<" "<<e.hour<<" "<<e.hRate<<'\n';}
}
}
data.close();
temp.close();
remove("tempfile.txt");
}
what i did is i get the E numbers of every record first then i sort them. then i open the file again and get the E numbers to compare to the sorted one then write it to the file.
but what happen is, it didn't sort, the files on the record remains the same :
5 reyes d r 1 2 3
3 delos d k 4 5 6
9 go t r 7 4 5
1 po w w 2 2 2
2 bun b m 3 3 3
Do anyone knows where in my code go wrong?