Phone Number List (NEED HELP WITH SEARCH)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mustang07
    New Member
    • Jul 2007
    • 2

    Phone Number List (NEED HELP WITH SEARCH)

    I need this program to find all occurences of a name and display them.
    If I enter Palmer, I need it to display all the Palmers in the list.
    Can anyone help me Please. Thank You!!

    [code=cpp]
    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
    const int NUMBERS = 11, S_LENGTH = 50;
    char list[11] [S_LENGTH] = {"Becky Warren 678-1223",
    "Joe Looney, 586-0097",
    "Geri Palmer, 223-8787",
    "Lynn Presnell, 887-1212",
    "Holly Gaddis, 223-8878",
    "Sam Wiggins, 486-0998",
    "Bob Kain, 586-8712",
    "Tim Haynes, 586-7676",
    "Warren Gaddis, 223-9037",
    "Jean James, 678-4939",
    "Ron Palmer, 486-2783"};

    char lookUp[S_LENGTH], *strPtr = NULL;
    int count;

    cout<< "\tPhone Number List\n\n";
    cout<< "Enter a name or partial name to search for: \n";
    cin.getline(loo kUp, S_LENGTH);

    for (count=0; count < NUMBERS; count++)
    {
    strPtr = strstr(list[count],lookUp);

    if (strPtr !=NULL)
    break;
    }

    if (strPtr == NULL)
    cout<< "No matching name was found.\n";
    else
    cout<< list[count]<<endl;



    system("PAUSE") ;
    return 0;
    }[/code]
    Last edited by sicarie; Jul 23 '07, 12:59 PM. Reason: Code tags
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    That code will find the first Palmer in the list, but since you break out of your loop as soon as the first match is found, you won't ever find more than one match.
    So you'll want to print out each match as it is found, and continue looping until there are no more names to check against.

    Hope this helps.

    Comment

    • archonmagnus
      New Member
      • Jun 2007
      • 113

      #3
      If you've already #included string, why not use strings (and the accompanying members of the string class) rather than character arrays?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Yes, and also why not use a vector<string>?

        Your lookup code is a duplication of STL code.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          mustang07-

          Please check your PM's (located at the top right of the page).

          Thanks,

          sicarie

          Comment

          Working...