472,119 Members | 2,067 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Help a newbie sort strings from a file?

9
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string> 
  5.  
  6. using namespace std;
  7.  
  8. void sort_LastName( string[], int );
  9. void sort_FirstName( string[], int );
  10. void sort_BirthYear( int[], int );
  11.  
  12. void main()
  13. {
  14.  
  15.     string firstName[100]; 
  16.     string lastName[100]; 
  17.     int birthYear[] = {0};
  18.      int numElements = 10;
  19.      int option; 
  20.      int count = 0; 
  21.      int i = 0; 
  22.  
  23.      ifstream inputFile( "input.txt", ios::in );
  24.  
  25.     if ( !inputFile )  
  26.    {
  27.       cout << "File 'input.txt' could not be opened" << endl;
  28.       exit( 1 );
  29.    }
  30.  
  31.  
  32.    while ( !inputFile.eof() )
  33.    {
  34.        inputFile >> firstName[count] >> lastName[count] >> birthYear[count]; 
  35.         count++;
  36.    }
  37.  
  38.     count--; 
  39.  
  40.     inputFile.close();    
  41.  
  42.     cout << "This program sorts names and birth years from input.txt. " << endl;
  43.  
  44.      do
  45.      {
  46.          cout << "\n(1) Sort by last name " << endl
  47.               << "(2) Sort by first name " << endl
  48.               << "(3) Sort by birth year " << endl
  49.               << "(4) Exit program " << endl << endl
  50.               << "Choose an option: ";
  51.          cin  >> option;
  52.  
  53.          if ( option == 1 )
  54.          {    
  55.             sort_LastName( lastName, numElements );
  56.  
  57.             for ( i = 0; i < numElements; i++)
  58.             {
  59.                 cout << "\n " << lastName[i] << ", " << firstName[i] << " " << birthYear[i] << endl;
  60.             }
  61.          }
  62.  
  63.          if ( option == 2 )
  64.          {
  65.              sort_FirstName( firstName, numElements );
  66.  
  67.              for ( i = 0; i < numElements; i++)
  68.             {
  69.                 cout << "\n " << firstName[i] << " " << lastName[i] << ", " << birthYear[i] << endl;
  70.             }
  71.          }
  72.  
  73.          if ( option == 3 )
  74.          {
  75.              sort_BirthYear( birthYear, numElements );
  76.  
  77.              for ( i = 0; i < numElements; i++)
  78.             {
  79.                 cout << "\n " << birthYear[i] << ", " << firstName[i] << " " << lastName[i] << endl;
  80.             }
  81.          }
  82.  
  83.          if ( option == 4 )
  84.          {
  85.              cout << "\n\n";
  86.              exit(1);
  87.          }
  88.  
  89.      } while ( option != 4 ); 
  90. }
  91.  
  92. // sort by last name A-Z
  93. void sort_LastName( string lastName[], int numElements )
  94. {
  95.     for ( int i = 0; i < numElements-1; i++ )
  96.     {
  97.         int minIndex = i; //stores index of the min array value
  98.  
  99.         for ( int j = i+1; j < numElements; j++ )
  100.             if ( lastName[j] < lastName[minIndex] )
  101.                 minIndex = j;
  102.  
  103.         //swap the strings at positions i and minIndex
  104.         string temp = lastName[minIndex];
  105.         lastName[minIndex] = lastName[i];
  106.         lastName[i] = temp;
  107.     }
  108.  
  109. }
  110.  
  111. // sort by first name A-Z
  112. void sort_FirstName( string firstName[], int numElements )
  113. {
  114.     for ( int i = 0; i < numElements-1; i++ )
  115.     {
  116.         int minIndex = i; //stores index of the min array value
  117.  
  118.         for ( int j = i+1; j < numElements; j++ )
  119.             if ( firstName[j] < firstName[minIndex] )
  120.                 minIndex = j;
  121.  
  122.         //swap the strings at positions i and minIndex
  123.         string temp = firstName[minIndex];
  124.         firstName[minIndex] = firstName[i];
  125.         firstName[i] = temp;
  126.     }
  127.  
  128. // sort by birth year 
  129. void sort_BirthYear( int birthYear[], int numElements )
  130. {
  131.     int startScan, minIndex, minValue;
  132.  
  133.     for ( startScan = 0; startScan < numElements - 1; startScan++ )
  134.     {
  135.         minIndex = startScan;
  136.         minValue = birthYear[startScan];
  137.  
  138.         for ( int index = startScan + 1; index < numElements; index++ )
  139.         {
  140.             if ( birthYear[index] < minValue )
  141.             {
  142.                 minValue = birthYear[index];
  143.                 minIndex = index;
  144.             }
  145.         }
  146.  
  147.         birthYear[minIndex] = birthYear[startScan];
  148.         birthYear[startScan] = minValue;
  149.     }
  150. }
  151.  
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!!!
Nov 19 '06 #1
2 3862
gonzo
9
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):
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string> 
  4.  
  5. using namespace std;
  6.  
  7. void sort_LastName( string[], string[], int[], int );
  8. void sort_FirstName( string[], string[], int[], int );
  9. void sort_BirthYear( string[], string[], int[], int );
  10.  
  11. void main()
  12. {
  13.  
  14.     string firstName[100]; 
  15.     string lastName[100]; 
  16.     int birthYear[] = {0};
  17.      int numElements = 10;
  18.      int option; 
  19.      int count = 0; 
  20.      int i = 0; 
  21.  
  22.      ifstream inputFile( "input.txt", ios::in );
  23.  
  24.     if ( !inputFile )  
  25.    {
  26.       cout << "File 'input.txt' could not be opened" << endl;
  27.       exit( 1 );
  28.    }
  29.  
  30.  
  31.    while ( !inputFile.eof() )
  32.    {
  33.        inputFile >> firstName[count] >> lastName[count] >> birthYear[count]; 
  34.         count++;
  35.    }
  36.  
  37.     count--; 
  38.  
  39.     inputFile.close();    
  40.  
  41.     cout << "This program sorts names and birth years from input.txt. " << endl;
  42.  
  43.      do
  44.      {
  45.          cout << "\n(1) Sort by last name " << endl
  46.               << "(2) Sort by first name " << endl
  47.               << "(3) Sort by birth year " << endl
  48.               << "(4) Exit program " << endl << endl
  49.               << "Choose an option: ";
  50.          cin  >> option;
  51.  
  52.          if ( option == 1 )
  53.          {    
  54.             sort_LastName( lastName, firstName, birthYear, numElements );
  55.  
  56.             for ( i = 0; i < numElements; i++)
  57.             {
  58.                 cout << "\n " << lastName[i] << ", " << firstName[i] << " " << birthYear[i] << endl;
  59.             }
  60.          }
  61.  
  62.          if ( option == 2 )
  63.          {
  64.              sort_FirstName( lastName, firstName, birthYear, numElements );
  65.  
  66.              for ( i = 0; i < numElements; i++)
  67.             {
  68.                 cout << "\n " << firstName[i] << " " << lastName[i] << ", " << birthYear[i] << endl;
  69.             }
  70.          }
  71.  
  72.          if ( option == 3 )
  73.          {
  74.              sort_BirthYear( lastName, firstName, birthYear, numElements );
  75.  
  76.              for ( i = 0; i < numElements; i++)
  77.             {
  78.                 cout << "\n " << birthYear[i] << ", " << firstName[i] << " " << lastName[i] << endl;
  79.             }
  80.          }
  81.  
  82.          if ( option == 4 )
  83.          {
  84.              cout << "\n\n";
  85.              exit(1);
  86.          }
  87.  
  88.      } while ( option != 4 ); 
  89. }
  90.  
  91. // sort by last name A-Z
  92. void sort_LastName( string lastName[], string firstName[], int birthYear[], int numElements )
  93. {
  94.     for ( int i = 0; i < numElements-1; i++ )
  95.     {
  96.         int minIndex = i; //stores index of the min array value
  97.  
  98.         for ( int j = i+1; j < numElements; j++ )
  99.             if ( lastName[j] < lastName[minIndex] )
  100.                 minIndex = j;
  101.  
  102.         //swap the strings at positions i and minIndex
  103.         string temp = lastName[minIndex];
  104.         lastName[minIndex] = lastName[i];
  105.         lastName[i] = temp;
  106.  
  107.         string temp2 = firstName[minIndex];
  108.         firstName[minIndex] = firstName[i];
  109.         firstName[i] = temp2;
  110.  
  111.         int temp3 = birthYear[minIndex];
  112.         birthYear[minIndex] = birthYear[i];
  113.         birthYear[i] = temp3;
  114.     }
  115.  
  116. }
  117.  
  118. // sort by first name A-Z
  119. void sort_FirstName( string lastName[], string firstName[], int birthYear[], int numElements )
  120. {
  121.     for ( int i = 0; i < numElements-1; i++ )
  122.     {
  123.         int minIndex = i; //stores index of the min array value
  124.  
  125.         for ( int j = i+1; j < numElements; j++ )
  126.             if ( firstName[j] < firstName[minIndex] )
  127.                 minIndex = j;
  128.  
  129.         //swap the strings at positions i and minIndex
  130.         string temp = firstName[minIndex];
  131.         firstName[minIndex] = firstName[i];
  132.         firstName[i] = temp;
  133.  
  134.         string temp2 = lastName[minIndex];
  135.         lastName[minIndex] = lastName[i];
  136.         lastName[i] = temp2;
  137.  
  138.         int temp3 = birthYear[minIndex];
  139.         birthYear[minIndex] = birthYear[i];
  140.         birthYear[i] = temp3;
  141.     }
  142.  
  143. // sort by birth year youngest-oldest
  144. void sort_BirthYear( string lastName[], string firstName[], int birthYear[], int numElements )
  145. {
  146.     int startScan, minIndex, minValue;
  147.  
  148.     for ( startScan = 0; startScan < numElements - 1; startScan++ )
  149.     {
  150.         minIndex = startScan;
  151.         minValue = birthYear[startScan];
  152.  
  153.         for ( int index = startScan + 1; index < numElements; index++ )
  154.         {
  155.             if ( birthYear[index] < minValue )
  156.             {
  157.                 minValue = birthYear[index];
  158.                 minIndex = index;
  159.             }
  160.         }
  161.  
  162.         birthYear[minIndex] = birthYear[startScan];
  163.         birthYear[startScan] = minValue;
  164.  
  165.         firstName[minIndex] = firstName[startScan];
  166.         firstName[startScan] = minValue;
  167.  
  168.         lastName[minIndex] = lastName[startScan];
  169.         lastName[startScan] = minValue;
  170.     }
  171. }
  172.  
  173.  
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
Nov 20 '06 #2
gonzo
9
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.
Nov 20 '06 #3

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 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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.