modifying this code to search an stringArray

Using C-String and String Classes


this needs to be done using the C-String Class.

•make a program called PROGRAM_1.CPP that has an array of 11 string objects that hold people's names and phone numbers. Use the following data:

•"Alejandra Cruz, 555-1223",

•"Joe Looney, 555-0097",

•"Geri Palmer, 555-8787",

•"Li Chen, 555-1212",

•"Holly Gaddis, 555-8878",

•"Sam Wiggins, 555-0998",

•"Bob Kain, 555-8712",

•"Tim Haynes, 555-7676",

•"Warren Gaddis, 555-9037",

•"Jean James, 555-4939",

•"Ron Palmer, 555-2783"

The program should ask the user to enter a name or partial name to search for in the array
Any entries in the array that match the string entered should be displayed.
For example, if the user enters "Palmer" the program should display the following names from the list:

•Geri Palmer, 555-8787

•Ron Palmer, 555-2783

This can be done using the C-String Class.

•Program 10-6, shown in the appendix to this Lab, shows a similar example, indicating how this needs to be done using the C-String Class.

•Using Program1.cpp, modify it so that it is able to do a search for the names in our contact list.

•Note that the Array used here is a two dimensional Array of char

•The first dimension (row dimension) represents the number of contacts in the Array

•The second dimension contains the Array of char holding the Contact information for one person


Tip: If you wrote Part 1 using the break command, and are having trouble with it, rewrite it so that you avoid using the break command




Part 2: Using C++ String and String Classes and find() method

MAKE A PROGRAM CALLED PROGRAM_2.CPP
•in other .cpp file make a program that has an array of 11 string objects that hold people's names and phone numbers. Use the following data:

•"Alejandra Cruz, 555-1223",

•"Joe Looney, 555-0097",

•"Geri Palmer, 555-8787",

•"Li Chen, 555-1212",

•"Holly Gaddis, 555-8878",

•"Sam Wiggins, 555-0998",

•"Bob Kain, 555-8712",

•"Tim Haynes, 555-7676",

•"Warren Gaddis, 555-9037",

•"Jean James, 555-4939",

•"Ron Palmer, 555-2783"


For this you will want to make use of the find() method shown below

Tip: If a string Member Function fails to perform the procedure it was asked to do, it

returns -1 to the program calling the function

Repeat this search using the C++ StringClass instead.

You may find that you will be able to execute this program using fewer steps than those used for the C-String class.

Note that the Array used here is a one dimensional Array of Strings.

Note also that you will be including the stringClass instead of the cstringClass.

Program1.cpp Use of strstr Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream> 
#include <string>
using namespace std;
int main()
{

// Constants for array lengths
    const int NUM_PRODS = 5;   // Number of products
    const int LENGTH = 27;  // String length
// Array of products
    char products[NUM_PRODS][LENGTH] =
    {
        "TV327 31 inch Television",
        "CD257 CD Player",
        "TA677 Answering Machine",
        "CS109 Car Stereo",
        "PC955 Personal Computer"
    };
    char lookUp[LENGTH];// To hold user's input
    char *strPtr = nullptr;                       // To point to the found product
    int index;// Loop counter
    bool found;// Found flag
// Prompt the usr for a product number.
    cout<< "\tProductDatabase\n\n";
    cout<< "Enter a product number to search for: ";
    cin.getline(lookUp, LENGTH);
// Search the array for a matching substring
    for (index = 0; index < NUM_PRODS; index++)
    {
        strPtr= strstr(products[index], lookUp );
        if (strPtr!= nullptr)
            break;
    }
// If a matching substring was found, display the product info.
    if (strPtr!= nullptr)
        cout<< products[index] << endl;
    else cout<< "No matching product was found.\n";
    return 0;
}

Other helpful sources that can help you


programB.cpp C++ String Version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// This program uses the find function to search an array of String.
#include <iostream>
#include <string>
using namespace std;
int main()
{
    // Constants for array lengths
    const int NUM_PRODS = 11;   // Number of products
    const int LENGTH = 27;     // String length
// Array of products
    string products[NUM_PRODS] =
    {
        "TV327 31 inch Television",
        "CD257 CD Player",
        "TA677 Answering Machine",
        "CS109 Car Stereo",
        "PC955 Personal Computer"
    };
    string lookUp;// To hold user's input
    int index; // Loop counter
    bool found = false;   // Found flag
    const int NOTFOUND = -1;

    cout<< "\tContactDatabase\n\n";
    cout<< "Enter a Product Number to search for: ";
    getline(cin,lookUp);
// Search the array for a matching substring
    for (index = 0; index < SIZE; index ++ )
    {
        if (stringArray[index].find(str,0)!= NOTFOUND)
        {
            found = true;
            break;
        }
    }
    if (found)
        cout<< stringArray[index] << endl;
    else
        cout<< str << " not found in table." << endl;
    cin.get();
    return 0;
}


OTHER HELPFUL INFO


 
mystring.find(str, x);

•Returns the first position at or beyond position x where the string stris found in mystring.

•strmay be either a string object or a character array.
SEE https://i.imgur.com/jwn616z.jpg for more
Last edited on
> make a program called PROGRAM_1.CPP that has an array of 11 string objects that hold people's names and phone numbers. Use the following data:
Right....

> "TV327 31 inch Television",
So you try and palm off on us some similar program that can easily be found by searching the web.
http://www.sci.brooklyn.cuny.edu/~ziegler/CIS15/Notes/pdf/CIS15_CH12.pdf
I got the first progam running using
1
2
#include <string>
#include <string.h> 

thanks!!
Topic archived. No new replies allowed.