Hey guys I've got a similar post on here but the previous one i didn't actually get any help this has to be handed in today and I'm stuck.
The situation is this:
- Read a text file into an array (most likely a struct array // which i can do for the most part //)
- Pass the array to a function (which is the area I'm having most of the trouble in)
- The function must then sort the array through a string field (using bubble sort)
- Pass the array back to the original function and have it stored in a new text file (which i can do easy)
I will just need you too check where I'm going wrong and maybe give some pointers of a code snippet to help me get pass this thanks (Ive got all the code if you want to run through it, what I'm showing is just the 2 functions of sort and archive.
//--------------------------------------------------------------------------
// void
// Purpose:
// Inputs:
// Ouputs:
//---------------------------------------------------------------------------
void sort(participant tempArray[], int size)
{
clrscr();
fstream recordFile;
fstream archiveFile;
recordFile.open("H:\\records.txt", ios::binary | ios::in);
participant temp;
int swaps;
int left;
int right;
//this was given by tutor (not my style of bubble but if it was suggested its what we should use)
do
{
swaps = 0;
left = 0;
right = 1;
while (right < size)
{
if (tempArray[right].name < tempArray[left].name)
{
temp = tempArray[left];
tempArray[left] = tempArray[right];
tempArray[right] = temp;
swaps++;
}
left++;
right++;
}
} while (swaps > 0);
cout << "The file has been sorted by name\n\n";
archiveFile.open("H:\\archive.txt", ios::binary | ios::in | ios::out);
recordFile.read((char*) &person, sizeof (participant));
while(!recordFile.eof())
{
archiveFile.write((char*) &person, sizeof(participant));
recordFile.read((char*) &person, sizeof (participant));
}
archiveFile.close();
cout << "\n---The file has been archived---";
getch();
}
//---------------------------------------------------------------------------
// void
// Purpose:
// Inputs:
// Ouputs:
//--------------------------------------------------------------------------
char archive()
{
clrscr();
fstream recordFile;
fstream archiveFile;
participant temp[100];
participant person;
char confirm;
int size = sizeof(temp);
cout << "**********ARCHIVE A RECORD**********\n\n" ;
recordFile.seekg(0, ios::beg);
cout << "Are you sure you want to archive the file?";
cin >> confirm;
if (tolower(confirm) == 'y' || tolower(confirm) == 'yes')
{
archiveFile.open("H:\\archive.txt", ios::binary | ios::out );
archiveFile.close();
cout << "\nPlease wait while we archive the record...\n\n";
recordFile.open("H:\\records.txt", ios::binary | ios::out | ios::in);
while(!recordFile.eof())
{
recordFile.read((char*) &temp[i], sizeof (participant));
i++;
sort(temp[], size);
}
recordFile.close();
return (temp[100], size);
}
else
{
cout << "\n---You have chosen NOT to archive this folder---";
recordFile.close();
getch();
return -1;
}
}
//---------------------------------------------------------------------------