Jump to content

C++ Search function

ExplOregon

Hello,

I was recently given a problem where I have to create a function that will "search" through an external data file for something based on a users input.  

In the case of this program, the person will input an age of animal they want and/ or what type of breed it is.  The program will in turn search for an 

animal that matches the criteria that the user inputs and display all of the animals that match the description.  I know I'm going to have to compare 

arrays and integers but frankly I have no idea how to do it.

Could someone explain the best way of approaching this, possibly throwing in some pseudocode to help explain? Thanks in advance

Code

Link to comment
Share on other sites

Link to post
Share on other sites

Well , you need to read the data from the input file and turn it into something with significance then list all the items needed based on the input of the user.

An animal is described like this :

<name> <breed> <age> <weight> <friendly> <descrpition>

So , for example , the input file will probably look something like :

Dog Pug 5 15 1 He is a pug!
Tulip Beagle 4 10 1 Yes , his name is actually Tulip.

You'll want to read the input file line by line , then get the meaningful information out of it. Example , the first string encountered on the line is the animal's name. The next is the breed. Then comes the age , weight , friendliness. The rest is the description. Or if you really want to make in interesting you could use something like a regex.

 

You store all that into a vector, list, whatever.

 

You process the user input and select the items from the vector which have the certain proprieties needed.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

As a not advanced programmer i will suggest a not advanced solution:

Lets say we create one dog:
 

input>> myVar[0].name;

input>> myVar[0].age;

input>> myVar[0].breed;

...

Then when user prompts the search function you could do like this :

 there is function called atoi() for converting char arrays to numbers. searchInput is a converted char array eg. atoi(searchInput) assuming that searchInput is a char array. if you use strings then it would look like this atoi(searchInput.str()) . str() converts a string to a char array. you can read more on atoi() on the web.

if(searchInput==integer) 
{
	//search by age or weight
	if(searchInput==myVar[0].age)
		//i guess print all info about the dog
	else if(searchInput == myVar[0].weight)
		//do the same
}
else if(searchInput!=integer)
{
	//check name, breed, friendly, description
	for(int i=0; i < searchInput.length(); i++)
   		{
         	myVar[0].name[i]==searchInput[i];
            match++;
        }
     // if all caracters match print what you want
}

This is not copy paste code though, just the idea. And another thing, this will only work if user entered search is identical to name, breed etc..

or you could use find() : http://www.cplusplus.com/reference/string/string/find/

 

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I guess what I really need to know how to do is read in the users input ie(Breed they want and age)

and then in turn compare it to the dogs in the data file and print the information of the dog, which I don't know how to do 

Link to comment
Share on other sites

Link to post
Share on other sites

Use comma separated values for the file database so you know the first one is always the breed, second one is always the name, etc.

 

"Affenpinscher", "Marcus", 2, 2, true, "short and curly hair"
"Afghan Hound", "Ronald", 1.4, 2.5, true, "very smooth hair"
"Afghan Shepherd", "Marvin", 5, 2, true, "likes to sleep on the couch"
...

You then read each line and store it to a string, split it using commas for the delimiter, and compare if all the user inputs match with the line that you just read. If so, print that.

 

For obtaining the input from the user, it's simpler if you ask on what the user will be searching by. i.e.: by breed, name, age, weight, friendliness, description.

Link to comment
Share on other sites

Link to post
Share on other sites

Is there a way to print a certain line of an external data file?

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, ExplOregon said:

Is there a way to print a certain line of an external data file?


You can std::getline to get an entire line as a string.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

On 2/14/2016 at 1:53 PM, Nineshadow said:


You can std::getline to get an entire line as a string.

I'm having a bit of trouble with this concept, I tried reading the c++ page on getline() and I'm getting a

syntax error when I try to test the concept,  I'm trying to get it to print the line of the animal when it finds a match with the breed.  Could you give some pointers? Code

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×