473,225 Members | 1,289 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 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 3944
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

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

Similar topics

4
by: VJ | last post by:
Hello All, The xml document below describes the contents of a folder in a Document Managment System. I need to retrieve the DocId for the most recently added (<Add_Date>) spreadsheet file(<name>...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
8
by: hothead098 | last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material) For this assignment you are to: 1) Create and manage arrays a) One of type integers (containing 10 elements). b) One of...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
2
by: XML Beginner | last post by:
I have an XML file that contains values that my application needs, so it knows which database to connect to. It also contains a configuration option so that I can specify which node to return...
7
by: DJ Dharme | last post by:
Hi, I really like to use stl as much as possible in my code. But I found it really hard to understand by looking into there source code. I have no idea about what iterator traits, heaps and...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.