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: -
-
#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;
-
}
-
}
-
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!!!
2 3862
Wish I could've edited my first post. =/
Anyways, I think I've figured out my main problem. Now can anyone tell me what I need to fix so that it doesn't output a load of random garbage characters everywhere? Here's the code, (it should compile and run fine): -
#include <iostream>
-
#include <fstream>
-
#include <string>
-
-
using namespace std;
-
-
void sort_LastName( string[], string[], int[], int );
-
void sort_FirstName( string[], string[], int[], int );
-
void sort_BirthYear( string[], string[], 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, firstName, birthYear, numElements );
-
-
for ( i = 0; i < numElements; i++)
-
{
-
cout << "\n " << lastName[i] << ", " << firstName[i] << " " << birthYear[i] << endl;
-
}
-
}
-
-
if ( option == 2 )
-
{
-
sort_FirstName( lastName, firstName, birthYear, numElements );
-
-
for ( i = 0; i < numElements; i++)
-
{
-
cout << "\n " << firstName[i] << " " << lastName[i] << ", " << birthYear[i] << endl;
-
}
-
}
-
-
if ( option == 3 )
-
{
-
sort_BirthYear( lastName, firstName, 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[], string firstName[], int birthYear[], 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;
-
-
string temp2 = firstName[minIndex];
-
firstName[minIndex] = firstName[i];
-
firstName[i] = temp2;
-
-
int temp3 = birthYear[minIndex];
-
birthYear[minIndex] = birthYear[i];
-
birthYear[i] = temp3;
-
}
-
-
}
-
-
// sort by first name A-Z
-
void sort_FirstName( string lastName[], string firstName[], int birthYear[], 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;
-
-
string temp2 = lastName[minIndex];
-
lastName[minIndex] = lastName[i];
-
lastName[i] = temp2;
-
-
int temp3 = birthYear[minIndex];
-
birthYear[minIndex] = birthYear[i];
-
birthYear[i] = temp3;
-
}
-
}
-
-
// sort by birth year youngest-oldest
-
void sort_BirthYear( string lastName[], string firstName[], 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;
-
-
firstName[minIndex] = firstName[startScan];
-
firstName[startScan] = minValue;
-
-
lastName[minIndex] = lastName[startScan];
-
lastName[startScan] = minValue;
-
}
-
}
-
-
Here are the contents of my 'input.txt', if someone wants to copy and paste it into their own .txt file to help me figue this out, (just a bunch of random names/dates):
John Doe 1968
Luke Smith 1974
Tyler Ray 1988
Mary Johnson 1982
Matt Leary 1955
Amanda Beckett 1970
Raymond Doriano 1960
Benjamin Brooke 1980
Maria Nunez 1948
Elizabeth Taylor 1966
D'oh! all I had to do was change the int[] to a string[]... What a pointless post.
Admin, please feel free to delete this post.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
4 posts
views
Thread by VJ |
last post: by
|
8 posts
views
Thread by baustin75 |
last post: by
|
4 posts
views
Thread by Pokerkook |
last post: by
|
11 posts
views
Thread by steve smith |
last post: by
|
8 posts
views
Thread by hothead098 |
last post: by
|
1 post
views
Thread by Rahul |
last post: by
|
48 posts
views
Thread by Alex Chudnovsky |
last post: by
|
2 posts
views
Thread by XML Beginner |
last post: by
|
7 posts
views
Thread by DJ Dharme |
last post: by
| | | | | | | | | | |