0

I want to be able to search an array of strings in C++. I have this data:

"Foo Becky, 924-334-2514", 
"Becky Warren, 555-1223", 
"Geri Palmer, 555-8787", 
"Ron Palmer, 555-2783"

If the user types Bec, the program finds the name Foo Becky, 924-234-2314. If the user types Palmer, then the program should show Geri Palmer, 555-8787 and Ron Palmer, 555-2783

Here is what I have so far:

#include <iostream>
#include <string>
using namespace std;
int main(){
    int n;
    string search;

    while(1){
        cout << "How many data you want to input: "<< endl;
        cin >> n;
        cin.ignore(1000, 10);

        if(n > 0 && n < 20){
            break;
        }
        cout << "Number of data can not be negative or more than 20. "<< endl;
    }

    string* data = new string[n];

    for(int i=0; i< n; i++){
        cout << "Enter [First Name] [Last Name], [Phone-Number] and then hit "
             << "enter." << endl << "e.g: Foo Becky, 925-245-413"<< endl;
        getline(cin,data[i]);
        cout << endl;
    }

    cout << "Enter what you want to search for: "<< endl;
    getline(cin, search);

    for(int i =0; i< n; i++){
        if(search == data[i]){
            cout << data[i]<< endl;
        }
    }
    delete [] data;
    return 0;
}

How do I search an array of strings in C++?

3
  • 5
    So what are your questions exactly? You have your code and tasks but no questions. Commented Mar 25, 2013 at 2:35
  • 3
    I'll echo what @SongWang said but I'll extrapolate your question based on your code. You have the basic logic more or less right, but using == won't work. You may want to read about std::string::find Commented Mar 25, 2013 at 2:36
  • My apologize that I didnt ask clearly. So my question is, how to make the program search what the user want to search in the array that they have filled before. But, although the user only search for, say "Palmer", the program must show both "Geri Palmer, 555-8787" and "Ron Palmer, 555-2783" Commented Mar 25, 2013 at 3:12

3 Answers 3

1

You have to use the find method of std::string. This function return the start postion of string searched for in the string searched in. If no match was found it returns npos witch is actually just -1.

if(data[i].find(search, 0) != std::string::npos);
{
    cout << data[i]<< endl;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thank you for replying. the code doesnt seem to work for me, plus I dont get it what does 0 for in (search, 0)
You really ought to read the comment I left. It answers your question if you would only care to follow the link to cplusplus.com/reference/string/string/find where you will find information on what the std::string::search function does and what the 0 means.
1

You should use find, as A4L already mentioned. I just want to add that your use of cin.ignore will not work well if bad value entered. you need

cin.clear() 

also. see this link for more details.

1 Comment

Thank you for the addition, my teacher has never said that before, that was pretty awesome
0

How to search an array of strings example in C++:

This is the brute force search method. Brute force means means we step through the entire string array and at each index of the array we search for our matching string.

#include<iostream>
#include<string>
using namespace std;
int main(){
    //Create a structure to hold the data:
    string data[] = {"pidgeon", "abcd", "1234", "%^*#"};

    //Get the length of the array.
    int size = 4;

    //loop through all the items and print them if they match
    string matchString = "bc";
    for(int x = 0; x < size; x++){
        if (data[x].find(matchString, 0) != std::string::npos){
            cout << data[x] << endl;
        }
    }
}

Above code prints:

abcd

You should be verbalizing the above code from top to bottom in your head like this:

An array of string called data is initialized to contain 4 elements and given four values pidgeon, abcd, 1234 and %^&#. An int variable called size is created which represents the number of elements in the string array. A string variable called matchString is created which contains the string 'bc'.

The for loop index starts at position zero and increments by 1 until it reaches one less than the size of the array. So the for loop goes through: 0, 1, 2, 3. The first value of x is 0. The if statement resolves data[0] to be pidgeon. The find method is applied against that string and two parameters are passed in. The string to be matched, and the position of the first character in the string to be considered in the search (0).

if 'bc' exists within 'pidgeon' then it will return the position of the first character of the first match, otherwise it will print std::string:npos which is the maximum value for size_t designating not found.

bc does not exist in pidgeon so it skips the interior of the for loop. The for loop continues to index position 1. bc is contained in abcd. So that string is printed. When all the items are searched, the for loop ends and the program completes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.