f2355116
f2355116

Reputation: 13

searching through a array of string

so i have this set of strings which are stored in an array i want to search the array so when the string is found it should say found and when its not found it should say invalid this is what i have so far

cout << "Enter a Name to Search" <<endl;
cin >>userInput;

for (int i=0; i<size; i++)
{
   if (first_name[i]==userInput)
    {
        cout <<"Found"<<endl;
    }else{
          cout << "InValid"<<endl;
            break;
         }  
}

so every time i run this i am always redirected to The else Statement is there anyway for me to fix this

Upvotes: 0

Views: 71

Answers (4)

Inline
Inline

Reputation: 2843

Use containers like std::set and std::unordered_set for fast searching.

#include <iostream>
#include <unordered_set>
#include <string>

int main()
{
    std::unordered_set<std::string> first_name;
    first_name.insert("Joe");
    first_name.insert("Anderson");
    //....

    std::string input;
    std::cin >> input;
    std::unordered_set<std::string>::iterator searchResult = first_name.find(input); // Search for the string. If nothing is found end iterator will be returned
    if(searchResult != first_name.end())
        std::cout << "Found!" << std::endl;
    else
        std::cout << "Not found!" << std::endl;
}

Programm output when "Joe" was typed:

Found!
Press <RETURN> to close this window...

For your example everything is okey, if userInput is std::string, first_name is array of std::string and variable size store array size.

Upvotes: 2

f2355116
f2355116

Reputation: 13

so i was able to find a solution this is what i did

    string result =""; //set a string named result 
    cout << "Enter a Name to Search" <<endl;
    cin >>userInput;

    for (int i=0; i<size; i++)
    {
       if (!(first_name[i]==userInput))
        {
           result = "Found";
            break;
        }else{
              result ="InValid";

             }  
    }
     cout <<result<<endl;  //outside of for loop

Upvotes: -1

Sathish V
Sathish V

Reputation: 378

You are breaking from the else part. So for instance if the array is size of 10, and if you give userinput as string present in 5th array element, your code will break at the first iteration of the for loop. Try the below code. If match found, It will print "found", or if the the userinput not there in the array it will print invalid. Hope it helps. Initialize the "first_name" with your array element and change the size.

string userInput;
string first_name[10];
int i=0;
int size = 10;

first_name[0] = "Hi";
first_name[1] = "Hii";
first_name[2] = "Hiii";
cout << "Enter a Name to Search" <<endl;
cin >> userInput;

for (i = 0; i<size; i++)
{
    if (first_name[i] == userInput)
    {
        cout <<"Found"<< endl;
        break;
    }
}
if(i == size)
    cout << "Invalid" << endl;

Upvotes: 1

Jaime
Jaime

Reputation: 5965

I think a more elegant solution would use a boolean flag, like:

cout << "Enter a Name to Search" <<endl;
cin >>userInput;
bool found = false;

for (int i=0; i<size; i++)
{
  if (first_name[i]==userInput)
  {
     found = true;
     break;
  }
}

cout << (found?"found":"invalid") << endl;

Upvotes: 0

Related Questions