468,545 Members | 1,853 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

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=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
Apr 22 '07 #1
3 1406
Ganon11
3,652 Expert 2GB
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.
Apr 22 '07 #2
[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
Apr 22 '07 #3
#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
Apr 23 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Rosy Moss | last post: by
7 posts views Thread by Brett Romero | last post: by
23 posts views Thread by Reggie | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.