[RESOLVED] c++ data structures program to search records by different fields
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: [RESOLVED] c++ data structures program to search records by different fields

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    10

    [RESOLVED] c++ data structures program to search records by different fields

    I work in a small library and have been given a task to make a program. To make things short, i have copied all my records to a text file

    Book name---------------Author
    1.Lord of the rings,J. R R Tolkiens
    2.Tom sawyer,,Mark twain
    3.Just after sunset,Stephen king
    4.The shining,Stephen king
    5.Oliver twist,Charles Dickens
    6.Huck Finn,Mark twain
    7.Children on Hurin,J R R Tolkiens
    8.Silmarilions,J R R tolkiens

    How can i make my c++ program to read this file, read records as arrays, sort them, and search either by book name or by the auhtor. If i write Tolkiends, how can i make it output all the books written by J R R tolkiens?

  2. #2
    Join Date
    May 2012
    Posts
    10

    Re: c++ data structures program to search records by different fields

    what I have done is this

    struct book // declare class
    {
    int no;
    string author; // type name of string
    string book; // type name of string
    string genre; // type name of string

    book* next; // type name of string
    book()
    {
    no=0;
    author=book=genre=" ";
    next=NULL; // next given FALSE value
    }



    void setnext(book *t)
    {
    next=t;
    }
    book* getnext()
    {
    return next;
    }
    };

    class linklist
    {
    book *start;
    book *current;
    public:
    linklist()
    {
    start=NULL;
    current=NULL;
    }

    void insert(book *tmp)
    {
    if(start==NULL)
    {
    start=tmp;
    }
    else
    {
    current=start;
    while(current->getnext()!=NULL)
    {
    current=current->getnext();
    }
    current->setnext(tmp);
    }
    }


    bool search_no(int value)
    {
    current=start;
    while(current!=NULL)
    {
    if(current->no==value)
    {
    cout<<current->author<<endl;
    cout<<current->book<<endl;
    cout<<current->genre<<endl;
    cout<<"---------------------------------------------- --------------------------"<<endl;
    return true;
    }
    else
    current=current->getnext();
    }
    return false;
    }

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: c++ data structures program to search records by different fields

    First of all. When you post code please embed it in code tags to preserve indentation [code]Paste your code here[/code]

    Second, since you use std::string you seem to be aware of stl so use a std::list instead of writing your own linked list.

    Here are two good reference sites http://cplusplus.com/ & http://cppreference.com
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    May 2012
    Posts
    10

    Re: c++ data structures program to search records by different fields

    My boss asks for atleast the file handling code till coming monday. Where as i have to submit the whole task ,on visual c++, by the end of next week! Sir, i dont have time to study the whole picture write now! Even much of this code i have taken help from source codes. Only if you could help me with this please, i will be able to complete my task!

  5. #5
    Join Date
    Jan 2009
    Posts
    596

    Re: c++ data structures program to search records by different fields

    Quote Originally Posted by smrizvi1 View Post
    I work in a small library and have been given a task to make a program.
    ...
    How can i make my c++ program to read this file,
    ...
    If this is really something you need for a 'small library', rather than a homework question, then just use a Spreadsheet or a Database. If you don't have one, you can get OpenOffice for free (http://www.openoffice.org/)

    Even if you don't currently know how to use a Spreadsheet/Database to do this, it will be a lot easier to learn this than trying to learn C++ in a few days. And the resulting application is much more likely to be bug-free.

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,880

    Re: c++ data structures program to search records by different fields

    Quote Originally Posted by smrizvi1 View Post
    My boss asks for atleast the file handling code till coming monday. Where as i have to submit the whole task ,on visual c++, by the end of next week! Sir, i dont have time to study the whole picture write now! Even much of this code i have taken help from source codes. Only if you could help me with this please, i will be able to complete my task!
    This forum frequently (and erroneously) gets taken for a sort of ambulance for half dead projects reanimation. In case you cannot drive a car, you just don't drive. If you have no time to study driving, you just don't drive. If you cannot do programming and have no time to study programming, you just don't do programming.
    Best regards,
    Igor

  7. #7
    Join Date
    May 2012
    Posts
    10

    Re: c++ data structures program to search records by different fields

    Code:
    what I have done is this
    
    struct book // declare class
    {
    int no;
    string author; // type name of string
    string book; // type name of string
    string genre; // type name of string
    
    book* next; // type name of string
    book()
    {
    no=0;
    author=book=genre=" ";
    next=NULL; // next given FALSE value
    }
    
    
    
    void setnext(book *t) 
    {
    next=t;
    }
    book* getnext()
    {
    return next;
    }
    };
    
    class linklist
    {
    book *start;
    book *current;
    public:
    linklist()
    {
    start=NULL;
    current=NULL;
    }
    
    void insert(book *tmp)
    {
    if(start==NULL)
    {
    start=tmp;
    }
    else
    {
    current=start;
    while(current->getnext()!=NULL)
    {
    current=current->getnext();
    }
    current->setnext(tmp);
    }
    }
    
    
    bool search_no(int value)
    {
    current=start;
    while(current!=NULL)
    {
    if(current->no==value)
    {
    cout<<current->author<<endl;
    cout<<current->book<<endl;
    cout<<current->genre<<endl;
    cout<<"---------------------------------------------- --------------------------"<<endl;
    return true;
    }
    else
    current=current->getnext();
    }
    return false;
    }

  8. #8
    Join Date
    Apr 1999
    Posts
    27,418

    Re: c++ data structures program to search records by different fields

    Quote Originally Posted by smrizvi1 View Post
    I work in a small library and have been given a task to make a program. To make things short, i have copied all my records to a text file
    What if the book's title has a comma in it? How would you create an entry, given that you're using a comma to separate the title and the author?
    How can i make my c++ program to read this file, read records as arrays,
    Here is where you start -- write a function that when given one of those lines in your file, separates each of the field into 3 fields. For example:
    Code:
    #include <string>
    
    struct BookInfo
    {
        std::string number;
        std::string title;
        std::string author;
    };
    
    BookInfo ParseRecord(const std::string& rec)
    {
        BookInfo b;
        // break up rec and store data in b;
        //...
        return b;
    }
    
    int main()
    {
         BookInfo myInfo = ParseRecord("1.Lord of the rings,J. R R Tolkiens");
    }
    When you have written this function then let's talk about linked lists. If you don't have this function, then why even try to code anything else? This is the first function you should have written, as nothing else matters if you don't have the ability to take a string and create a BookInfo from it.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 12th, 2012 at 01:12 PM.

  9. #9
    Join Date
    May 2012
    Posts
    1

    Re: [RESOLVED] c++ data structures program to search records by different fields

    my task is to make a structure to store the record of library. The record must consist of at least following fields: Title, Author, Edition, Price, Publisher, Category.
    -Define functions authorSearch ( ), TitleSearch ( ) and CategorySearch( ) to search a book with respect to author, title and category. [There can be more than one books, written by one author, in one category]
    The program must be menu driven with the following choices:
    1> Add new record
    2> Search a book w.r.t author
    3> Search a book w.r.t Title
    4> Search a book w.r.t category
    5> Exit
    The program must be able to store at least 50 records.
    plz help me

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center