Phone DB app c++

Pages: 12
I need that name to be displayed based on search string. For example if the Phone DB has a name "Tenzin John" and if I type only the first or last full name, it returns the name. But if I search by string like "Te" or "Jo" matching strings, nothing gets displayed. This string matching must not be case sensitive.

Below is my code, but it doesn't work as expected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

void SearchforNames(string Names[], int total, string UserInput, vector<string> &First, vector<string> &Last) 
{
	int pos;
	int spos;			 	
	for (int i = 0; i < total; ++i)
	{
		
		pos = Names[i].find(UserInput);

		spos = Names[i].find(" ");
			
		// If pos == -1 than continue, name isn't in the list.
		if( pos == -1)
		{
			continue; 
		}
		
		// If pos != -1 than the name is found.
		else  
		{
		}
					
		}

		// If pos < spos, then it's a last, push into vector.
		if( pos < spos)
		{   
		    int i;
		    Last.push_back(Names[i]);	
		}
		int i;
		int pos2 = Names[i].find(spos, UserInput);
		if( pos2 > spos)
		{
			First.push_back(Names[i]);
		}
		if( pos > spos)
		{
			 First.push_back(Names[i]);
		}									 
}
if( pos == -1)

Where did you get this idea that find would return -1 ??????
Where did you get this idea that find would return -1 ??????

It's guaranteed that
std::string::npos == std::numeric_limits<std::size_t>::max() == std::size_t(-1)
So (this part of the code) is correct, if awful.
Last edited on
is it awful? Its ensured so you can do exactly that, or am I off base? Otherwise, why define a value for it at all: force them to use the wordy text every single time.
Yeah I guess it's not so bad.

C++11:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <string>
#include <regex>
#include <vector>
#include <iostream>

// looks for case-insensitive matches for beginning of first names, last names
// invariant: names are of the form first_name white space last_name ie. ^\S+\s+\S+$, user_input is alphanumeric
void search_for_names( const std::string names[], std::size_t num_names, std::string user_input,
                       std::vector<std::string>& matched_first_names, std::vector<std::string>& matched_last_names )
{
    const std::regex first_name_re( user_input + ".*",  std::regex::icase ) ; // begins with user_input
    std::regex last_name_re( "\\S+\\s+" + user_input + ".*",  std::regex::icase ) ; // second name begins with user_input

    matched_first_names.clear() ;
    matched_last_names.clear() ;

    // if( user_input.empty() ) return ; // uncomment if empty input does not match the beginning of any string

    for( std::size_t i = 0 ; i < num_names ; ++i )
    {
        if( std::regex_match( names[i], first_name_re ) ) matched_first_names.push_back( names[i] ) ;
        if( std::regex_match( names[i], last_name_re ) ) matched_last_names.push_back( names[i] ) ;
    }
}

int main() // a minimal test driver
{
    std::string names[] = { "Bugs Bunny", "Daffy Duck", "PORKY   Pig", "pig porky", "Elmer Fudd", "Daft Daffodil", "Coy coyote", "Wile Coyote" } ;
    const std::size_t N = sizeof(names) / sizeof(*names) ;

    for( const std::string input : { "BUN", "bu", "daf", "pork", "udd", "COY", "" } )
    {
        std::cout << "input: \"" << input << "\"\n" ;

        std::vector<std::string> first_names ;
        std::vector<std::string> last_names ;

        search_for_names( names, N, input, first_names, last_names ) ;

        std::cout << '\t' << first_names.size() << " #matching first name\n" ;
        if( !first_names.empty() ) for( const std::string& name : first_names ) std::cout << "\t\t" << name << '\n' ;
        std::cout << '\t' << last_names.size() << " #matching last name\n" ;
        if( !last_names.empty() ) for( const std::string& name : last_names ) std::cout << "\t\t" << name << '\n' ;
        std::cout << "-------------\n" ;
    }
}

http://coliru.stacked-crooked.com/a/742619e311b050d8
is it awful? Its ensured so you can do exactly that, or am I off base? Otherwise, why define a value for it at all: force them to use the wordy text every single time.


Magic numbers are a code smell, though for simple homework it's probably not a big deal.
Topic archived. No new replies allowed.
Pages: 12