Thread: Better Search

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103

    Better Search

    Hello, I've been doing a program for my dad, so it could help him work better. I've been trying to make a function that finds a string or words that the user inputs. Here is what I got:

    vstore sturcture:
    Code:
    struct storage
    {
    	char name[100];
    	char id[100];
    	char description[100];
    }temp;
    Code:
    void inline Buscar()
    {
    	
    	int counter=0;
    	char string[100];
    
    	cout<<"Entre la palabre que desea encontrar: "<<endl;
    	cin.getline(string,100,'\n');
    
    	for(int x=0; x<vstore.size(); x++)
    	{
    		for(int index=0; index<strlen(string); index++)
    		{
    			if(toupper(vstore[x].name[index]) == toupper(string[index]) )
    			{
    				counter++;
    			}
    			if( counter == strlen(string) )
    			{
    				global = vstore[x];
    				cout<<"Name: "<<vstore[x].name;
    				cout<<"\nID: "<<vstore[x].id;
    				cout<<"\nDesc.:"<<vstore[x].description<<endl;
    			}
    			else
    			{
    				cout<<"Didn't Find a MATCH"<<endl;
    
    			}
    		}
    	}
    	system("PAUSE");
    	return;
    }
    How can you make a more efficient code? As so far this one works Please someone help...

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Sorry It's in Spanish...

    Here it's in english:
    Code:
    void inline Find()
    {
    	
    	int counter=0;
    	char string[100];
    
    	cout<<"Word to find: "<<endl;
    	cin.getline(string,100,'\n');
    
    	for(int x=0; x<vstore.size(); x++)
    	{
    		for(int index=0; index<strlen(string); index++)
    		{
    			if(toupper(vstore[x].name[index]) == toupper(string[index]) )
    			{
    				counter++;
    			}
    			if( counter == strlen(string) )
    			{
    				global = vstore[x];
    				cout<<"Name: "<<vstore[x].name;
    				cout<<"\nID: "<<vstore[x].id;
    				cout<<"\nDesc.:"<<vstore[x].description<<endl;
    			}
    			else
    			{
    				cout<<"Didn't Find a MATCH"<<endl;
    
    			}
    		}
    	}
    	system("PAUSE");
    	return;
    }

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Why are you checking letter by letter? If you want an exact match (which is what your program is looking for), you might as well compare the full word. Secondly, you should be using C++ style strings. They can make it more efficient. Lastly, and this is minor, it could speed up your program if you made your search word uppercase outside of the loop and kept it that way. Right now, your program has to call toupper() twice every time it loops.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    How can I compare the C++ strings?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    C++ style strings can be compared through regular arithmetic operators.

    Code:
    std::string foo = "Fu";
    std::string bar = "bar";
    std::string fubar = "Fubar";
    
    if (fubar == foo + bar)
       std::cout << "Strings are fun.";
    Last edited by SlyMaelstrom; 01-29-2006 at 01:25 AM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Thanx SlyMaelstrom, Now I love C++ string!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM