Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Nov 2006
Posts: 14
#1: Apr 22 '07
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=42;
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's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Apr 22 '07

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


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.
Newbie
 
Join Date: Nov 2006
Posts: 14
#3: Apr 23 '07

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


[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.fail()==false)
for loop------
outputfile << name[MAXNAMES][LISTLGTH]; <Here is my problem I don't understand person class or struct
Newbie
 
Join Date: Nov 2006
Posts: 14
#4: Apr 23 '07

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


#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.fail() == false) // Test to see if file was successfully opened
{
ShowArray ( Array);
SortArray (Array, pass);
ShowArray ( Array);
outputData.close();
}
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
Reply