Thread: Finding names that start with a certain letter

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Fort Pierce, FL
    Posts
    3

    Finding names that start with a certain letter

    I am doing a homework assignment. I do not want the complete answer, just a hint. I have to load a file of names into an array, display the names, then sort the names alphabetically, display them again, then list how many names start with certain letters. I have everything done but cannot for the life of me figure out how to display how many names start with a certain letter. Here are the names Collins, Bill
    Smith, Bart
    Allen, Jim
    Griffin, Jim
    Stamey, Marty
    Rose, Geri
    Taylor, Terri
    Johnson, Jill Allison, Jeff
    Looney, Joe
    Wolfe, Bill
    James, Jean
    Weaver, Jim
    Pore, Bob
    Rutherford, Greg
    Javens, Renee
    Harrison, Rose
    Setzer, Cathy
    Pike, Gordon
    Holland, Beth
    And here is the code I have so far. Any hints would be awesome :-)

    thanks!



    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <algorithm>
    
    
    
    
    using namespace std;
    
    
    string name [20];
    
    
    void load_name()
    {
        int i=0;
    fstream in_file;
    in_file.open("names.txt");
            do
        {
            getline(in_file,name[i]);
            i++;
        }
            while (i<=20);
    
    
    
    
    }
    void no_sort()
    {
    
    
    int i=0;
    
    
    cout << "Here are the unsorted names:\n";
    cout << "--------------------------\n";
            do
        {
            cout << name[i];
            cout << "\n";
            i++;
        }
            while (i<=20);
    
    
    }
    void sorter()
    {
     string temp[20];
          for(int i = 1; i <= 20; i++)
         {
              for (int j=0; j < (20 -1); j++)
             {
                if (name[j+1] < name[j])      
                   { 
                        temp[j] = name[j];           
                        name[j] = name[j+1];
                        name[j+1] = temp[j];
                                      
                   }
              }
         }
    }
    void sorted()
    {
    
    
    int i=0;
    
    
    cout << "Here are the sorted names:\n";
    cout << "--------------------------\n";
            do
        {
            cout << name[i];
            cout << "\n";
            i++;
        }
            while (i<=20);
    
    
    }
    void howmany()
    {
    
    
    }
    
    
    int main ()
    {
        load_name();
        no_sort();
        sorter();
        sorted();
        howmany();
    
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    It's not clear what the goal is for "how many names start with a certain letter". Are you just supposed to ask for a letter and then respond with how many names start with that specific letter, or are you supposed to cycle through all possible letters and report the number of names for each letter?

    If you're supposed to report the number of names for all letters, you could use a loop inside a loop that goes through every letter on the outer loop, or you could use a single loop to create a "histogram" array of integers indexed by letter, where each integer is a count of the number of names that start with the letter corresponding to the index of the "histogram" array, and then another single loop to output the results of the "histogram".

  3. #3
    Registered User
    Join Date
    May 2013
    Location
    Fort Pierce, FL
    Posts
    3
    It is supposed to output something like
    Total Names that Begin with A: 2
    **************************
    Allen, Jim
    Allison, Jeff

    And so on.


    Sorry, did not mean to be vague.

    Thank you!

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Fitzgibbon View Post
    It is supposed to output something like
    Total Names that Begin with A: 2
    **************************
    Allen, Jim
    Allison, Jeff

    And so on.
    So by "and so on", I assume you mean that you're supposed to output all the names that start with 'B', then 'C', then 'D', ... . In this case, generating a histogram would be a good approach, and you would learn how to implement a histogram.

  5. #5
    Registered User
    Join Date
    May 2013
    Location
    Fort Pierce, FL
    Posts
    3
    I am going to look into that, thank you!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You've got buffer overflows everywhere:
    while (i<=20);

    Array indexes start from 0 ... length - 1.
    I suggest you read this: SourceForge.net: Safer arrays in Cpp - cpwiki

    Also, avoid global variables and more consistent indenting couldn't hurt.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-18-2011, 07:51 PM
  2. finding names of files in a directory(dos)
    By vaibhav in forum C++ Programming
    Replies: 1
    Last Post: 04-21-2006, 08:36 PM
  3. Finding User names.....
    By twomers in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2005, 04:46 AM
  4. Replies: 2
    Last Post: 04-29-2002, 11:52 PM
  5. Finding Letter Grade and Marital Status
    By ProgrammingDlux in forum C++ Programming
    Replies: 7
    Last Post: 03-28-2002, 12:05 AM