Search function in c++?

Hello, today i come to you all in need of guidence! I've googled and seen some stuff but it all kinda confuses me as it's a bit of a jump from where i left off in c++ but is there a guide or anything out there explaining a SEARCH function i don't think im advanced enough to try to connect a DB with info to a program so instead i want to do it from the program itself so my goals
1. Get this working
2. Maybe try to make it a actual program not console application?
3. Have a search function where if you type search (X Y Z) or in a search bar put (X Y Z) it will bring up something in the code that matches EG
Search term: Hammers
brings up
Ballpoint Hammer
Reindhardt Hammer
Other Names of Hammers
 
None currently as it all kinda revolves around this search function


Any help is greatly appreciated as i am yet to find a good guide explaining simply how searching works in c++


Another thing i ponder:
The ability to add to a program like this?
So they can do something in the program that will somehow add info they input so eg:
add new hammer1 so next time they search for hammers they will also see new hammer 1
I thought of putting it all in a text file and the program just searches through the text file and outputs it or adds it to the text file but i figured there's a better way
Last edited on
You can try the find() member function of std::string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main()
{
    std::string sRef = "Hammers";
    std::string sSearch1 = "Ballpoint Hammer";

    for (size_t length = 0; length <=1; ++length)//searching the entire string & a substring one size short
    {
        std::size_t found = sSearch1.find(sRef.substr(0, sRef.size()-length));
        //check whether string being searched has the substring of the reference string

        if (found!=std::string::npos)
        {
            std::cout << "Found " << sRef.substr(0, sRef.size()-length) << " within " << sSearch1 << '\n';
        }
        else
        {
            std::cout << "No match for " << sRef.substr(0, sRef.size()-length)<< '\n';
        }
    }
}

Output
1
2
No match for Hammers
Found Hammer within Ballpoint Hammer


Few points:
1. you can determine at what point to stop searching the reference string by varying the boundary condition for length
2. currently the search is case sensitive, you can make it case insensitive by converting both strings to lower or upper case throughout first and then searching
3. if you consider proceeding with this approach your next steps might be: (a) putting the code into a function, (b) testing it against a std::vector<std::string>, (c) searching files, (d) building a stand-alone program out of it


This is already my idea @gunnerfunner i want to have like a text box on a GUI application they enter the search term and then click search and then it will query a databse to try to find it so in an example something like the hammers or a customer database so you click search on the application and then it searches the database for all customers named Alex and outputs their first name last name and some other notes in the DB

OR if i can have it built into the application somewhere, i'd just rather not have a text file have to follow around the .exe for it to work and since i am already trying to figure out a way to do a login if i learn how to incorporate DB into C++ then kinda two birds one stone
Last edited on
learn how to incorporate DB into C++

depends on which DB you want to use, most major ones have C++ support, e.g:
https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-introduction.html
https://docs.oracle.com/database/121/LNCPP/relational.htm

You may also find the following link of interest - the program in question here is Python but the general discussion about whether/when to move from storing data in text files to a DB is still relevant for you:
http://softwareengineering.stackexchange.com/questions/209123/when-should-use-of-database-be-preferred-over-parsing-data-from-a-text-file

Last edited on
Topic archived. No new replies allowed.