If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Search book by its author from 1st record to last

Hey everyone! i stuck here with a thing. I use this code to search a book by its author. I have used the approach of bubble sorting then binary searching.

Alright, now this is what i have. what i want to do now is to search a particular BOOK by its author without using the bubble sorting or binary searching method and instead search all the records one at a time, from start till the end for its author. I think the improved approach will make the coding a lot shorter than it is now

Re: Search book by its author from 1st record to last

What's your question?

Sorting and binary searching will be much more efficient with a large dataset. While you want to write concise code, favoring small code over efficient isn't usually a good idea.

A better approach would be just to sort your list as you build it. When somebody adds a book, use a binary search to find the appropriate place to add it. Your list will be sorted without an explicit sort function.

You certainly don't want to sort every time somebody does a search. Once you sort it, it should stay sorted.

Re: Search book by its author from 1st record to last

Nobody's going to write the code for you. You can store the books in an array or list and just traverse it until you find the book you want. Since users can add books, a list would be a better container. Your text book should have a chapter on them.

In the long run, my suggestion is better, and since you don't know how to do it your way either, I'm not sure the comment that you don't know how to do it my way is valid.

You didn't write the code in your first post I take it? Simple array or list traversal is much simpler than anything in the OP.

Re: Search book by its author from 1st record to last

Alright, now this is what i have. what i want to do now is to search a particular BOOK by its author without using the bubble sorting or binary searching method

1) It would help if you changed from those char arrays to string variables such as std::string.

Then your C++ code doesn't have ugly C-string handling functions such as strcpy(), strcmp(), etc. Whenever I see that in an obvious beginner program, it raises the question of what material you're using to learn C++.

2) Nothing in your code is specific to Visual C++. Your question should have been posted in the non-Visual C++ forum.

Now, I didn't compile this, but this is how you would do the sort and search without writing any loops, search code, etc. in C++. You use the std::sort() algorithm and find_if() algorithm functions. The sort is reduced to a single line of code and a function, and the find_if() is a single line of code, using a function object.

As GCDEF mentioned, no one is going to write all the code for you. I showed an example using a radically different, but "real-world" C++ approach. Now your goal is to understand what I posted, as I won't go into explanation. Any good book on C++ will explain it to you. If it's an assignment, you will need to explain it to your teacher, as they will ask you what those functions do and how you used them.

Re: Search book by its author from 1st record to last

Thank you Mr. Paul
I also wanted to know that is it possible search a particular string/char, having more than one word, by just a single word?? I mean If i want to search 'Lord of the rings' and i just input 'rings', is there any way that i get all the books that contain the word 'rings'?

Re: Search book by its author from 1st record to last

Originally Posted by smrizvi1

Thank you Mr. Paul
I also wanted to know that is it possible search a particular string/char, having more than one word, by just a single word?? I mean If i want to search 'Lord of the rings' and i just input 'rings', is there any way that i get all the books that contain the word 'rings'?