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