Converting unsorted info from a file then alphabetically to a new file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gemacjr1201
    New Member
    • Nov 2006
    • 14

    Converting unsorted info from a file then alphabetically to a new file

    I need to open a file that has unsorted list of names and birthdates formated like this: LASTNAME, FIRST$MM/DD/YYYY\n.It could have as much as 100 names. I will use a selection sort algorithm to alphabetize the list and store it in a new file named by the user. So how do I read the data from the file? Then take that information using a selection sort algorithm to alphabetize the list and store it in a new file? I know it's a 2D array
    const int MAXNAMES=100;
    const intLISTLENGTH=4 2;
    NAMES[MAXNAMES][LISTLENGTH]

    SOME psuedocode:
    open file to be read
    test to see if it opened
    read data from file
    sort data by alphabet
    close file
    open input file
    input sorted data

    ANY HINTS WOULD BE MUCH APPRECIATED!! Thanks
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    All of this sounds good...have you come across any trouble yet?

    When you are inputting data, you will have to separate each piece of information from the others. I would suggest making a Person class or struct that you can overload the < operator for - this will help immensely when doing your selection sort. Also, do you have to use selection sort? There are much quicker sorting algorithms out there - one such algorithm is the Quicksort.

    Comment

    • gemacjr1201
      New Member
      • Nov 2006
      • 14

      #3
      [QUOTE=Ganon11]All of this sounds good...have you come across any trouble yet?

      When you are inputting data, you will have to separate each piece of information from the others. I would suggest making a Person class or struct that you can overload the < operator for - this will help immensely when doing your selection sort. Also, do you have to use selection sort? There are much quicker sorting algorithms out there - one such algorithm is the Quicksort.[/QUOTE


      No I don'y have to use selection sort.
      I am having trouble reading the data out of the unsorted file.
      I know that I need to write a for loop and use:


      ofstream outputfile
      outputfile.open ("namelist.txt" )
      if(outputfile.f ail()==false)
      for loop------
      outputfile << name[MAXNAMES][LISTLGTH]; <Here is my problem I don't understand person class or struct

      Comment

      • gemacjr1201
        New Member
        • Nov 2006
        • 14

        #4
        #include <iostream>
        #include <iomanip>
        #include <fstream>
        using namespace std;
        const int MAXNAMES=100;
        const int LISTLENGTH=61;
        const int pass=100;
        void ShowArray (char Array[MAXNAMES][LISTLENGTH]);
        void SortArray (char Array[MAXNAMES][LISTLENGTH], int pass);

        int main()
        {
        ofstream outputData; // Create an input file stream object
        char Array[MAXNAMES][LISTLENGTH];

        outputData.open ("outputest.txt "); // Open the file named outputtest.txt

        if(outputData.f ail() == false) // Test to see if file was successfully opened
        {
        ShowArray ( Array);
        SortArray (Array, pass);
        ShowArray ( Array);
        outputData.clos e();
        }
        else // The file open was unsuccessful
        {
        cout << "\nError: unable to open file.\n";
        }

        return 0;
        }

        void ShowArray (char Array[MAXNAMES][LISTLENGTH])
        {
        for (int i=0; i<MAXNAMES; i++)
        {
        for (int j=0; j<LISTLENGTH; j++)
        {
        cout << Array[i][j];
        }
        cout << endl;
        }
        }
        void SortArray (char Array[MAXNAMES][LISTLENGTH], int pass)
        {
        bool swap;
        int temp[LISTLENGTH];

        do
        {
        swap=false;
        for (int i=0; i<MAXNAMES-1; i++)
        {
        if (Array[i][0]>Array[i+1][0])
        {
        for (int j=0; j<LISTLENGTH; j++)
        {
        temp[j]=Array[i][j];
        Array[i][j]=Array[i+1][j];
        Array[i+1][j]=temp[j];
        swap=true;
        }
        }
        }
        } while(swap);
        }
        Hers is some of my test code

        Comment

        Working...