RSS Forums RSS
Please support our C++ advertiser: Programming Forums

Input Output File Streams - Sorting Help With Array

Join Date: Jun 2005
Posts: 4
Reputation: myprecious is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
myprecious myprecious is offline Offline
Newbie Poster

Re: Input Output File Streams - Sorting Help With Array

  #5  
Jun 10th, 2005
Global fstream is part of the specification from my tutor...same with binary file. issue with eof loop?????

I got it too read to an array of structures..

//---------------------------------------------------------------------------
void readData()
{
   const int MAX = 30;
   person recordArray[ MAX ];
   person temp;
   int i;

   clrscr();

   MyFile.seekg( 0, ios::beg );

   MyFile.read( ( char* ) &recordArray[ 0 ], sizeof( person ) );

   i = 0;
   while( !MyFile.eof() )
   {
      cout << recordArray[ i ].id << "  ";
      cout << recordArray[ i ].firstName << "  ";
      cout << recordArray[ i ].lastName << "  ";
      cout << recordArray[ i ].age << "  ";
      cout << recordArray[ i ].height << "  ";
      cout << recordArray[ i ].dateTime << endl;

      i++;
      MyFile.read( ( char* ) &recordArray[ i ], sizeof( person ) );
   }

   sortArray( recordArray, MAX );
   MyFile.clear();
   getch();
}
//---------------------------------------------------------------------------
void sortArray( person recordArray [], int m )
{
   int position;
   const int MAX = 3;
   person tempArray[ MAX ];

   for ( int i = 0; i < 5; i++ )
   {
      cout << recordArray[ i ].id << "  ";
      cout << recordArray[ i ].firstName << "  ";
      cout << recordArray[ i ].lastName << "  ";
      cout << recordArray[ i ].age << "  ";
      cout << recordArray[ i ].height << "  ";
      cout << recordArray[ i ].dateTime << endl;
   }

   for ( int i = 0; i < m - 1; i++ )
   {
      position = i;
      for ( int j = i; j < m; j++ )
      {
         if ( recordArray[ j ].age < recordArray[ position ].age )
            position = j;
      }
      if ( recordArray[ position ].age < recordArray[ i ].age )
      {
         tempArray[ 0 ] = recordArray[ i ];
         recordArray[ i ] = recordArray[ position ];
         recordArray[ position ] = tempArray[ 0 ];
      }
   }

   for ( int i = 0; i < 5; i++ )
   {
      cout << recordArray[ i ].id << "  ";
      cout << recordArray[ i ].firstName << "  ";
      cout << recordArray[ i ].lastName << "  ";
      cout << recordArray[ i ].age << "  ";
      cout << recordArray[ i ].height << "  ";
      cout << recordArray[ i ].dateTime << endl;
   }

   getch();
}
//---------------------------------------------------------------------------

In sortArray, its doing an Exhange sort... When it swaps and I print recordArray, it prints garbage.. I want to sort this on the height field.. this is a float, that returned an error, so now im doing it on the age field... can you help?
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:59 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC