Thread: Searching for multiple items in an array

  1. #1
    Unregistered
    Guest

    Question Searching for multiple items in an array

    i'm trying to search an array of ten strings so that when the user enters the name to search for in the array, it will return all instances of that name. this is the code i have written that returns only ONE hit.

    #include <iostream.h>
    #include<string.h>

    void main(void)
    {
    char NamePhone[11][30] = {"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[30], *StrPtr = NULL;
    cout << "\tPhone Number List\n\n";
    cout << "Please enter the name whose phone number you wish to search for: ";
    cin.getline(LookUp, 30);
    for (int Index = 0; Index < 11; Index++)
    {
    StrPtr = strstr(NamePhone[Index], LookUp);
    if (StrPtr != NULL)
    break;
    }
    if (StrPtr == NULL)
    cout << "No matching name found.\n";
    else
    cout << NamePhone[Index] << endl;
    }


    can anybody help me modify this code so it returns multiple instances of the name being searched for?
    for example if i types the name Palmer, it should come out like Geri Palmer, 223-8787
    Ron Palmer, 486-2783

    hope this doesn't confuse anybody, thanks!

    -=metpage=-
    [email protected]

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if (StrPtr != NULL)
    break;

    you dont want to break from the loop here. remove those lines.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Simply iterate through the array as a whole (aka, don't break) and then print out the record when you find a match inside the loop.
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    if you want to do it the way you started, you can use something to this effect:


    #include <iostream.h>
    #include<string.h>

    void main(void)
    {
    char NamePhone[11][30] = {"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[30];
    char match[11][30] = { NULL };
    int matches = 0, len = 0,a = 0;
    cout << "\tPhone Number List\n\n";
    cout << "Please enter the name whose phone number you wish to search for: ";
    cin.getline(LookUp, 30);

    for (int i = 0; i < 11; i++)
    {
    if (strstr(NamePhone[i],LookUp) != NULL)
    {
    len = strlen(NamePhone[i]);
    for (int x = 0; x < len; x++)
    {
    match[a][x] = NamePhone[i][x];
    }
    ++a;
    ++matches;
    }
    }
    if (matches)
    {
    for (i = 0; i < matches; i++)
    {
    cout << "match found: " << match[i] << endl;
    }
    }
    else
    {
    cout << "No matches found." << endl;
    }

    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching between array indicies
    By Asagohan in forum C Programming
    Replies: 7
    Last Post: 04-27-2005, 12:28 AM
  2. Adding items to an Array in Visual Basic 6
    By ct26torr in forum Windows Programming
    Replies: 1
    Last Post: 04-25-2003, 04:39 AM
  3. Not getting good result searching an array of string.
    By cazil in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2002, 11:24 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM
  5. Structure Array searching
    By ling in forum C Programming
    Replies: 4
    Last Post: 10-18-2001, 12:39 PM