Aug 8, 2015 at 4:52pm UTC
Problem :
--->I cannot sort the records in the file
CONSUMER.DAT
--->Help me with this part of code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
void sort()
{
BILL a;
int x[100];
int *p=x;
int count=0;
ifstream fin("CONSUMER.DAT" ,ios::binary);
ofstream fout("REPORT.DAT" ,ios::binary);
fin.seekg(0);
while (fin.read((char *)&a,sizeof (a)))
{
count++;
*p=a.get_sub_no();
p++; // <-------------there is a problem here
}
int temp,pos,small;
for (int i=0;i<(count-1);i++)
{
small=x[i];
pos=i;
for (int j=i+1;j<count;j++)
{
if (x[j]<small)
{
small=x[j];
pos=j;
}
}
temp=x[i];
x[i]=x[pos];
x[pos]=temp;
}
fin.seekg(0);
for (i=0;i<count;i++)
{
while (fin.read((char *)&a,sizeof (a)))
{
if (x[i]==a.get_sub_no())
fout.write((char *)&a,sizeof (a));
break ;
}
}
fin.close();
fout.close();
cout<<"\nSorting Was Successful !!!" ;
}
please help me in this part !!!
Note:
--->Compiler : Turbo C++ for Windows 7
--->Version : 3.7.8.9m_r
--->By : Neu Tron
Last edited on Aug 8, 2015 at 4:56pm UTC
Aug 8, 2015 at 6:28pm UTC
> there is a problem here
¿what problem?
¿how did you detect it?
lines 16--32: unless your sort is the problem, I don't want to analyse it
std::sort(x, x+count);
lines 34--42: files are not circular, once you reach the end of the file you can't keep reading.
> a.get_sub_no();
I cannot compile that, cannot run it, cannot debug it.
At most you can get code review by tired eyes.
Aug 13, 2015 at 9:14pm UTC
in line 45: while (fin.read((char *)&a,sizeof (a)))
when that loop ends the file will be in an invalid state, so the fin.seekg(0);
(line 54) will fail.
You may fin.clear()
before the seek to make it valid again, so the reading in line 73 would work.
However, your algorithm is incorrect, suppose that the smallest element is in the last register of the archive, then that would be the only one writed to the output file.