Ok I got the code to work but with some errors. First when i print out the aorted array to the file it is sorted escept for one number in the beginning second it gives me a negative number whihc i believe is a pointer how can i fix this? I'll include a copy of the print out.
Thanks

#include <iostream>				    // Need for cout,cin
#include <iomanip>                   // Need setf,fixed,showpoint,setprecision
#include <stdio.h>				    // Need for getchar
#include <fstream>					// Needed for files
#include <cstdlib>					// Needed for exit function
#include <string>					// Need for string class


using namespace std;


string getInputFileName(); // a function to prompt for the complete file name
void bubblesort(ifstream& inFile, ofstream& outFile);


int main ()
{
	int num[501];

	ofstream outFile;
	ifstream inFile;
	string fileName; // complete file name including the path

	fileName = getInputFileName(); // prompt and obtain the full file name

	// try to open the file
	outFile.open(fileName.c_str(),ios_base::binary | ios::out| ios::app);
	inFile.open(fileName.c_str(),ios_base::binary);

	if(!outFile.is_open())
	{
		cerr << "File open Error Creating file" ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit(1);
	}

	if (!inFile.is_open())
	{
		cerr << "File open error " ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit (1);
	}












	//int maxSize;

	bubblesort( inFile, outFile);
	outFile.close();
	inFile.close();





	// keeps program opening till user enters a key

	cout.setf (ios::showpoint );
	cout.setf( ios::fixed);
	cout << setprecision(2);

	cout << "\n\n Press Enter to continue" << endl;
	cin.ignore();
	char ch = getchar();


	return 0;

}




//************************************************************
//
// Function name: Search
//
// Purpose: Locate the ID the user entered. If found displays it with the name if not asks to enter into to the file
//             
//
// Input parameters: string id, filehandle passed by reference (ifstream &inFile, ofstream &outFile)
//
// Output parameters: none
//
// Return Value: 
//               
//
//************************************************************
void bubblesort(ifstream& inFile, ofstream& outFile )
{ 
	int i ;	
	int k = 0;
	int num[501];
	int count = 0;


	while(!inFile.eof() ) // Input The Numbers from The File Into The Array NumbersIn[]
	{ // While i<100 And While File Is Not End Of File. 
		inFile>>num[k];
		k++;
	}

	int j;
	int  maxSize = 501;
	for ( i=1; i< maxSize; i++)
	{ 
		for ( j = maxSize-1;j>i;j--)
		{
			if(num[j] < num[j-1])
				swap ( num[j-1],num[j]);
			count++;
		}
	}
	for (int i = 0; i < maxSize; i++)
	{	
		outFile << num[i] << "    "u   << endl;



	}
	outFile << " You swapped numbers" << "  " << count << "  " << "times" <<endl;
}
//************************************************************
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of a file
//              i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
//               of a file
//
//************************************************************

string getInputFileName()
{
	string f_Name; // fully qualified name of the file

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}

here is the printed to file output. I removed a bunch of the numbers since there is 500 of them but the first two as you can see are wrong.

15437
-858993460
74
85
262
353
362
434
465
480
481
534
658
691
717
739
747
813
856
928
1017
1039
1083

You swapped numbers 124750 times

Well, it doesn't work for 500 numbers. Are there actually 500 numbers in the file? If not, you shouldn't be sorting 500 numbers. Does it work for something less than 500? Is -858993460 even in the file? If not, look at that.

There is reading in an array from a file and there is sorting that array. In at least one of these two areas, something has gone wrong. Your first job is to figure out which one.

Well, it doesn't work for 500 numbers. Are there actually 500 numbers in the file? If not, you shouldn't be sorting 500 numbers. Does it work for something less than 500? Is -858993460 even in the file? If not, look at that.

There is reading in an array from a file and there is sorting that array. In at least one of these two areas, something has gone wrong. Your first job is to figure out which one.

Well there is 500 numbers and i got them all sorted now the only things is the negative number in front. I'm not sure where it comes from I believe its the mem location for "i" i think but not sure. thats the only thing wrong

Well there is 500 numbers and i got them all sorted now the only things is the negative number in front. I'm not sure where it comes from I believe its the mem location for "i" i think but not sure. thats the only thing wrong

ok i figured out its not from writing to the outfile. I cout before the outfile writing and its there so its somewhere in the sorting or reading the file

Well there is 500 numbers and i got them all sorted now the only things is the negative number in front. I'm not sure where it comes from I believe its the mem location for "i" i think but not sure. thats the only thing wrong

Use something smaller. Don't change anything else and have a file of , say, three or four numbers. Same code, see what happens.

Use something smaller. Don't change anything else and have a file of , say, three or four numbers. Same code, see what happens.

ok got it to work just changed array size and max size to 500. i put 501 for the null character but its was giving garbage. Thanx for the help

ok got it to work just changed array size and max size to 500. i put 501 for the null character but its was giving garbage. Thanx for the help

That wasn't the problem as far as I can tell. You were sorting maxSize numbers rather than k numbers. k is the actual number of numbers and is never used in the code as far as I can tell.

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.