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

}
//---------------------------------------------------------------------------

Recommended Answers

All 6 Replies

I'm not surprised you didn't get any help, since you never explained what the problem is.

if you check the top message i said what im having problems with. i have put it in a list lableing where my problem is... no offence but read

First, you should fill these in!!

// void
// Purpose:
// Inputs:
// Ouputs:

They will really help your structure/logic.

I am concerned that your sort() function should not be doing anything with fstream - you should read the data elsewhere and pass it to sort().

I would highly recommend using std::vector over an array - it will certainly help you pass the data around.

Dave

yea thanks... how would i implement that... as of the the //void etc

it pretty much does what the functions say they do... the one sorts the other archives

my issue is reading the file into an array and passing it how would that be done?

while (!recordfile.eof))

or

if (recordFile.eof())

or

for (int i = 0; i < recordFile.eof(); i++)

and then how would i pass it

would i need to change void to participant anywhere, and what do i pass?

You should avoid using .eof() as your while condition. I can't remember the post where they do a good job explaining it but you should be able to find it. for your while condition I would do

while (recordFile.read((char*) &temp[i], sizeof (participant)))
{
    //...
}

this will make sure your while loop will read everything in the file because as soon the read function fails it will end the loop.

yup ok thanks and how would i pass the (struc) participant (vairable) temp[100] through to sort now... would i have to change void archive or what beacuse i tried to make it participant* archive but it made massive errors occur...

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.