0

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

3
  • Please edit your question to provide minimal reproducible example Commented Aug 9, 2016 at 4:16
  • You can walk this code on paper (or in your head) and see what happens if userInput is the second entry in the first_name array. Commented Aug 9, 2016 at 4:16
  • 1
    Provide minimal code to reproduce your problem. How size is declared? Commented Aug 9, 2016 at 4:34

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

it looks kinda complex for me
@f2355116 I can add fix for arrays, if you edit your question and add the minimal code to reproduce problem. How size is declared?
1

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;

Comments

0

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;

4 Comments

i guess you are correct but may you explain what is that question mark here does cout << (found?"found":"invalid") << endl;
thats a C/C++ ternary operator (see cprogramming.com/reference/operators/ternary-operator.html ) that I used for avoiding an if statement.
okay i get it its kinda shortcut way of writing if then else statement
yes, it increases the readability in most cases. So, was that what you were looking for?
-1

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

1 Comment

looks like your break is in the wrong block. It should break whenever you find the string

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.