Thread: Search a stuct

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Search a stuct

    I read in a file into a struct. all is fine, but i want to search for a last name within the struct. I can search for a last name but the problem is in the struct i have 2 same authors like.
    Code:
    C++ Programming: From Problem Analysis...  Malik
    C++ Programming: Program Design Including... D. S. Malik
    when i do a search for Malik i get only the first one.
    I would like to get both results. heres what i have so far

    Code:
    struct Library {
    
    string Title;
    string Author;
    };
    
    const int ARRAY_SIZE = 1000;
    Library books [ ARRAY_SIZE ];
    to search
    Code:
    int showBooksByAuthor( int count, string name )
    {
    
    for(int i = 0; i < count; i++) {
       
         if( books[i].Author.find(name) == 0)
         cout << books[i].Title << " (" << books[i].Author << ")" << endl;
    }
    what can i do to return both?
    sorry about the double post and misspelling

  2. #2
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I had some luck with
    Code:
    if(name.find_first_of(books[i].Author)== 0)
    
    output:
    
    Enter file name with the extension: c:\library.txt
    14 records loaded successfully.
    Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All:
    A
    Authors Name: Malik
    C++ Programming: From Problem Analysis... (Malik)
    Beginning LINUX Programming (Stones and Matthew)
    C++ Programming: Program Design Including... (D. S. Malik)
    Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All:
    but as you can see it picked up a record i didnt not ask for..

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    try:

    if (books[i].find(name) != string::npos)

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Thank you that did the trick.
    I dont know much about npos but i guess it's time i learn.

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Just read the documentation for the string class.

    http://www.cplusplus.com/reference/s...ring/find.html
    Return:
    The position of the first occurrence in the string of the searched content.
    If the content is not found, the member value npos is returned.

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