简体   繁体   中英

Finding a string element in an array C++

I have found other topics like this, but none of them are working, it keeps returning -1 regardless what I type. Idk why, thanks!

#include <iostream>
#include <string>
using namespace std;

int find(string arr[], int len, string seek)
{
    for (int i = 0; i < len; ++i)
    {
       if (arr[i].find(seek) != string::npos) {
        return i;
       }
       return -1;
    }
}


int main() {

    string info[] = { "Becky Warren, 678-1223","Joe Looney, 586-0097","Geri Palmer , 223-887","Lynn Presnell, 887-1212","Holly Gaddis, 223-8878","Bob Kain, 586-8712","Tim Haynes, 586-7676","Warren Gaddis, 223-9037", "Jean James , 678-4939","Ron Palmer, 486-2783" };

    cout<<"Please enter a name and you will be returned a phone number"<<endl;
    string name;
    cin>>name;
    //for (int i = 0; i<sizeof(info)/sizeof(info[i]);i++) {
        int x = find(info,10,name);
        cout<<x;
        //cout<<info[x]<<endl; 

    //}
    system("pause");
}

You can use the function std::string::find available in the standard C++. See this link for an example on how to use it.

In particular it is needed to develop two main steps:

  1. in the function find that you have written you find the index of the array (or vector as in the following example) containing that name;
  2. Then you need to find inside the string found the position of the comma (through find function of standard library) and then extract the substring containing the number which is located two positions after the comma. You can do this using substr function.

Below a sample code using vector instead of array.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int find(vector<string> arr, string seek)
{
    for (int i = 0; i < arr.size(); ++i)
    {
       if (arr.at(i).find(seek) != string::npos) {
        return i;
    }
   return -1;
    }
}

int main()
{
    vector<string> info;
    info.push_back("Becky Warren, 678-1223");
    info.push_back("Joe Looney, 586-0097");
    info.push_back("Geri Palmer , 223-887");
    info.push_back("Lynn Presnell, 887-1212");
    info.push_back("Holly Gaddis, 223-8878");
    info.push_back("Bob Kain, 586-8712");
    info.push_back("Tim Haynes, 586-7676");
    info.push_back("Warren Gaddis, 223-9037");
    info.push_back("Jean James , 678-4939");
    info.push_back("Ron Palmer, 486-2783" );

    cout<<"Please enter a name and you will be returned a phone number"<<endl;
    string name;
    cin>>name;

    // x is the string in the array containing the name written by the user
    int x = find(info, name);

    // Check if at least one was found
    if(x == -1)
    {
        cout << "Name " << name << " not found." << endl;
        return -1;
    }

    // extract number from the found string (find comma position and then take a substring starting from that position until the end
    int commaPosition = info.at(x).find(",");
    string phoneNumber = info.at(x).substr(commaPosition+2);
    cout<<"Phone number found: " << phoneNumber<< endl;

    system("pause");

    return 0;
}

Your problem is that your return - 1 should be after for loop. When you check whether your string is somewhere in your array you do it only one time, because of the fact that after an if statement the program sees return - 1 which means "oh I should exit the function now and return -1".

for (int i = 0; i < len; ++i)
 {
     if (arr[i].find(seek) != string::npos)return i; 
 }
 return -1;

When you got such a simple problems I recommend you to use debugger which will tell you exactly what you are doing in your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM