-1

I am trying to search a structure for a value I entered. However I want the search to work even if I only enter a part of the word. How do I do this?

Here is my Code:

for (int i = 0; i < size; i++)
{
    if (searchName == ptrCandy[i].name)
    {
      cout << "Name: " << ptrCandy[i].name << "\n" << "Quantity: " << ptrCandy[i].quantity;
      cout << "\n" << fixed << setprecision(2) << "Cost: $" << ptrCandy[i].cost << "\n" << endl;
    }
}
pippin1289
  • 4,861
  • 2
  • 22
  • 37

4 Answers4

1

I am assuming that if you type in n characters, you want to only match candy elements that begin with those n characters, and not attempt to do any autocorrection or spell-checking or the like. If these are correct assumptions, then let the STL do the work for you:

int searchLength = searchName.length();
for( int i=0; i<size; i++ ) {
    if ( ptrCandy[i].name.length() >= searchLength &&
         searchName == ptrCandy[i].name.substr( 0, searchLength ) ) {
    // Found it!
    }
}

For example, if you have a candy named "snickers" and "snack mix", then if you enter "sn", it will return both of these, but if you enter "sni", you will only get "snickers".

Ken P
  • 552
  • 3
  • 11
0

first, you need a threshold for how close the match has to be. is 1 letter ok?

second, decide if it must match from the beginning, or beginning of a word eg does do work for "odor"?

Then if you don't want to use another library, loop through each char and continue until you reach the threshold or the end of the string

In other words, if the name is "Long dude one" and the search string is "dude", then start at name[0] and do a 4-iteration loop (four letters in dude) and check each one with the corresponding one from name. If any letter does not match, exit that loop. Then do the same thing starting from name[1] to name[4], then from name[2] to name[5] etc all the way until you are checking the last 4 letters of the search string " one" against dude. However, you wouldn't get that far because on the 6th attempt, eg looping through name[5] to name[8] all 4 letters would match so you would set match=true and exit.

put that in a function and call it for each name.

AwokeKnowing
  • 7,728
  • 9
  • 36
  • 47
0

Assuming these types are of std::string:

for (int i = 0; i < size; i++)
{
    size_t pos = ptrCandy[i].name.find(searchName);
    if (pos != string::npos)
    {
      cout << "Name: " << ptrCandy[i].name << "\n" << "Quantity: " << ptrCandy[i].quantity;
      cout << "\n" << fixed << setprecision(2) << "Cost: $" << ptrCandy[i].cost << "\n" << endl;
    }
}

If you wanted to do it case-insensitive, just convert both strings to either all upper or all lower case and then do the search on the modified strings.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
0

You can use member function compare the following way

for (int i = 0; i < size; i++)
{
    if ( ptrCandy[i].name.compare( 0, searchName.size(), searchName ) == 0 )
    {
      cout << "Name: " << ptrCandy[i].name << "\n" << "Quantity: " << ptrCandy[i].quantity;
      cout << "\n" << fixed << setprecision(2) << "Cost: $" << ptrCandy[i].cost << "\n" << endl;
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335