Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:09:23+00:00 2026-06-16T01:09:23+00:00

Using the find method of the string class and I’m not getting the correct

  • 0

Using the find method of the string class and I’m not getting the correct results in my query. Here is my code

int main()
{
    string phoneData;
    string name;
    string phoneNumbers[51];
    ifstream inputFile;
    inputFile.open("phonebook");
    int i = 0;
    while (getline(inputFile, phoneData))
    {
        phoneNumbers[i] = phoneData;
        i++;
    }
    cout << "Enter a name or partial name to search for: ";
    getline(cin, name);
    cout << endl << "Here are the results of the search: " << endl;

    for(int i =0;i<50;i++)
    {
        if (name.find(phoneNumbers[i]) == 0)
            cout << phoneNumbers[i] << endl;
    }
    inputFile.close();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:09:24+00:00Added an answer on June 16, 2026 at 1:09 am

    you aren’t using it correctly. string::find() returns the beginning position when it finds a match, or string::npos if it doesn’t find a match. you also have the search backwards. you’re looking for ‘name’ inside ‘phoneNumbers[i], not the other way around. your check inside the loop should look like this:

    if (phoneNumbers[i].find(name) != string::npos)
        cout << phoneNumbers[i] << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I find the length of a String without using the length() method
I am using entity framework and I can't find include method like in this
I find myself using this method a ton to perform actions based on a
I've got the following JSON string to deserialize: [{application_id:1,application_package:abc},{application_id:2,application_package:xyz}] I'm using DataContractJsonSerializer method. It
I am not sure about the correct way to deal with method expressions in
I am using proguard to obfuscate the code. I am getting the following error
The Java String class has a matches(String regex) method that checks whether the current
I am working with some complex queries using the dynamic find_all method and reached
I'm using find for a task and I noticed that when I do something
I'm working on using Find/Replace to change a bunch of labels to DataBound text.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.