Hi,
Long time lurker, first time poster.
I have been working on this assignment for days and keep getting stuck. I am supposed to read the contents of a text file (first and last names, e.g. Smith, John ) into a two dimensional array, sort the array, and write it to a new file. Names longer than 25 characters are supposed to be truncated.
I am storing the names in a character array of pointers, I am able to input the names into the array but as soon as my while loop exits my array is totally messed up.
Here's my code:
Code:
//Names are found in the file "names.txt"
#include <iostream>
using std::cout;
using std::cin;
using std::ios;
using std::cerr;
using std::endl;
#include <iomanip>
using std::setw;
#include <fstream>
using std::ifstream;
using std::ofstream;
void sortFile( char *[][1], int );
void getFileName( char [] );
void writeFile ( char *[][1], char [], int );
int main ()
{
char *names[25][1]; //array to hold first and last names
char filename[25]; //array to hold filename input
int row = 0;
char fname[25]; //array to hold filename
char buffer[50] = ""; //array to hold buffer
char temp[26] = ""; //array to hold temporary value of buffer
cout << "Enter filename to read from: ";
cin >> setw(24) >> fname; //input filename
ifstream inFile( fname, ios::in ); //open file for input
if ( !inFile ) { //if filename is not valid ask for another name
cout << "File could not be opened." << endl;
while ( !inFile ) {
cout << "Enter filename: ";
cin >> fname;
ifstream inFile( fname, ios::in );
}
}
while ( inFile.peek() != EOF ) {
inFile.getline(buffer, 50);
if (strlen(buffer) > 26) {
strncat(temp, buffer, 26);
names[row][0] = temp;
}
else { names[row][0] = buffer;}
cout << names[row][0] << endl;
row++;
inFile.clear();
}
sortFile( names, row ); //call function to sort file
getFileName( filename ); //call function to input filename
writeFile( names, filename, row ); //call function to write to file
return 0;
}
void sortFile( char *n[][1], int num ){
char *tmp; //pointer to hold temporary value
for(int i = 0; i < num; i++) {
if(n[i][0] > n[i + 1][0]) { //if current array element is larger than next element swap them
//swap last name
tmp = n[i][0];
n[i][0] = n[i + 1][0];
n[i + 1][0] = tmp;
//swap first name
tmp = n[i][1];
n[i][1] = n[i + 1][1];
n[i + 1][1] = tmp;
}
}
}
void getFileName( char fname[] ) {
char response;
cout << "Enter filename to write to: ";
cin >> setw(24) >> fname; //input filename
ofstream inFile( fname, ios::out ); //open file for output
if ( inFile ) { //if file already exists ask if it should be overwritten
cout << "File already exists. Overwrite (y or n)?";
cin >> response;
if ( response == 'n' ) { //if no ask for another filename
cout << "Enter filename to write to: ";
cin >> setw(24) >> fname;
}
}
}
void writeFile( char *n[][1], char fname[], int num ) {
cout << num << endl;
int row = 0; //initialize counter
ofstream outFile( fname, ios::out ); //open file for output
if (!outFile ) { //if file could not be opened display error and exit
cerr << "File could not be opened." << endl;
exit( 1 );
}
while ( row < num ) { //output names to file while number of names is greater than counter
cout << n[row][0];
outFile << n[row][0] << endl;
row++; //increase counter
}
}
I will be so grateful if you can give me some advice.
Thank you! Jenna