So I have a project where I'm supposed to have a .txt input file of no more than ten first names, last names and birth years, and than in a menu I'm to give the user some options as to how the strings can be sorted. My problem is kind of hard to understand, so please bear with me. I've figured out how to sort the last names, first names and birth years individually, but how do I make it so that all 3 things match up correctly so that the first names, last names and birth years match up like they do in the file? Here's my code:
Aside from matching up names/birth years, whenever I run the program I get a odd display of characters popping up. Obviously it's something weird with whatever's stored in the memory. How do I fix that? Thanks!!!
Code:
#include <iostream> #include <fstream> #include <string> using namespace std; void sort_LastName( string[], int ); void sort_FirstName( string[], int ); void sort_BirthYear( int[], int ); void main() { string firstName[100]; string lastName[100]; int birthYear[] = {0}; int numElements = 10; int option; int count = 0; int i = 0; ifstream inputFile( "input.txt", ios::in ); if ( !inputFile ) { cout << "File 'input.txt' could not be opened" << endl; exit( 1 ); } while ( !inputFile.eof() ) { inputFile >> firstName[count] >> lastName[count] >> birthYear[count]; count++; } count--; inputFile.close(); cout << "This program sorts names and birth years from input.txt. " << endl; do { cout << "\n(1) Sort by last name " << endl << "(2) Sort by first name " << endl << "(3) Sort by birth year " << endl << "(4) Exit program " << endl << endl << "Choose an option: "; cin >> option; if ( option == 1 ) { sort_LastName( lastName, numElements ); for ( i = 0; i < numElements; i++) { cout << "\n " << lastName[i] << ", " << firstName[i] << " " << birthYear[i] << endl; } } if ( option == 2 ) { sort_FirstName( firstName, numElements ); for ( i = 0; i < numElements; i++) { cout << "\n " << firstName[i] << " " << lastName[i] << ", " << birthYear[i] << endl; } } if ( option == 3 ) { sort_BirthYear( birthYear, numElements ); for ( i = 0; i < numElements; i++) { cout << "\n " << birthYear[i] << ", " << firstName[i] << " " << lastName[i] << endl; } } if ( option == 4 ) { cout << "\n\n"; exit(1); } } while ( option != 4 ); } // sort by last name A-Z void sort_LastName( string lastName[], int numElements ) { for ( int i = 0; i < numElements-1; i++ ) { int minIndex = i; //stores index of the min array value for ( int j = i+1; j < numElements; j++ ) if ( lastName[j] < lastName[minIndex] ) minIndex = j; //swap the strings at positions i and minIndex string temp = lastName[minIndex]; lastName[minIndex] = lastName[i]; lastName[i] = temp; } } // sort by first name A-Z void sort_FirstName( string firstName[], int numElements ) { for ( int i = 0; i < numElements-1; i++ ) { int minIndex = i; //stores index of the min array value for ( int j = i+1; j < numElements; j++ ) if ( firstName[j] < firstName[minIndex] ) minIndex = j; //swap the strings at positions i and minIndex string temp = firstName[minIndex]; firstName[minIndex] = firstName[i]; firstName[i] = temp; } } // sort by birth year void sort_BirthYear( int birthYear[], int numElements ) { int startScan, minIndex, minValue; for ( startScan = 0; startScan < numElements - 1; startScan++ ) { minIndex = startScan; minValue = birthYear[startScan]; for ( int index = startScan + 1; index < numElements; index++ ) { if ( birthYear[index] < minValue ) { minValue = birthYear[index]; minIndex = index; } } birthYear[minIndex] = birthYear[startScan]; birthYear[startScan] = minValue; } }
Comment