Cant debug that error is in adding recored or in displayig

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shan

    Cant debug that error is in adding recored or in displayig

    #include<fstrea m>
    #include<iostre am>
    #include<string >
    #include<iomani p>
    #include<stdlib .h>
    #include<conio. h>
    #include<stdio. h>

    using namespace std;
    class library
    {
    private:
    struct lib
    {
    char flag;
    int id;
    string title;
    string subject;
    }books;

    fstream file;

    public:
    library();
    void addrec();
    void listrec();
    void modifyrec();
    void delrec();
    void recallrec();
    void packrec();
    void searchid();
    void searchttl();
    void exit();
    };


    //Zero argument constructor
    library::librar y()
    {
    file.open("libr ary",ios::binar y|ios::in|ios:: out);
    if(!file)
    {
    cout<<endl<<"Un able to open file.";
    exit();
    }
    }

    //Add record
    void library::addrec ()
    {
    char ch;
    file.seekp(0L,i os::end);
    do
    {
    cout<<endl<<"En ter book id :";
    cin>>books.id;
    cout<<endl<<"En ter book title :";
    cin>>books.titl e;
    cout<<endl<<"En ter book subject :";
    cin>>books.subj ect;

    books.flag=' ';
    file.write((cha r *)&books,sizeof (books));
    cout<<"Add another record? (Y/N)";
    cin>>ch;

    }
    while(ch=='y'|| ch=='Y');
    }

    //List all records
    void library::listre c()
    {
    int j=0;

    file.seekg(0L,i os::beg);

    while(file.read ((char*)&books, sizeof(books)))
    {
    if(books.flag!= '*')
    {
    cout<<endl<<"Re cord NO:"<<j++;
    cout<<endl<<"ID is "<<books.id ;
    cout<<endl<<"Su bject is "<<books.subjec t;
    cout<<endl<<"Ti tle is "<<books.ti tle;
    }
    }


    file.clear();
    cout<<endl<<"Pr ess any key...";
    getch();
    }

    //Modify a record
    void library::modify rec()
    {
    int tempid;
    int count=0;
    long int pos;
    cout<<"Enter id of book to modify"<<endl;
    cin>>tempid;

    file.seekg(0L,i os::beg);

    while(file.read ((char*)&books, sizeof(books)))
    {
    if(books.id==te mpid)
    {
    cout<<"Enter new record"<<endl;
    cout<<endl<<"En ter book id"<<endl;
    cin>>books.id;
    cout<<endl<<"En ter book title"<<endl;
    cin>>books.titl e;
    cout<<endl<<"En ter book subject"<<endl;
    cin>>books.subj ect;
    books.flag=' ';

    //place pointer at record which has to
    //be over written

    pos=count*sizeo f(books);
    file.seekp(pos, ios::beg);
    file.write((cha r*)&books,sizeo f(books));
    return;
    }
    count++;
    }

    cout<<endl<<"No book with id "<<tempid<< " is in record";
    cout<<endl<<"Pr ess any key...";
    getch();

    file.clear();
    }

    //Mark a record for deletion
    void library::delrec ()
    {
    int tempid;
    int count=0;
    long int pos;
    cout<<"Enter id of book to delete"<<endl;
    cin>>tempid;

    file.seekg(0L,i os::beg);

    while(file.read ((char*)&books, sizeof(books)))
    {
    if(books.id==te mpid)
    {
    books.flag='*';

    pos=count*sizeo f(books);
    file.seekp(pos, ios::beg);
    file.write((cha r*)&books,sizeo f(books));
    return;
    }
    count++;
    }

    cout<<endl<<"No book with id "<<tempid<< " is in record";
    cout<<endl<<"Pr ess any key";
    getch();

    file.clear();
    }

    //Recall the record
    void library::recall rec()
    {
    int tempid;
    int count=0;
    long int pos;
    cout<<"Enter id of book "<<endl;
    cin>>tempid;

    file.seekg(0L,i os::beg);

    while(file.read ((char*)&books, sizeof(books)))
    {
    if(books.id==te mpid)
    {
    books.flag=' ';

    pos=count*sizeo f(books);
    file.seekp(pos, ios::beg);
    file.write((cha r*)&books,sizeo f(books));
    return;
    }
    count++;
    }

    cout<<endl<<"No book with id "<<tempid<< " is in record";
    cout<<endl<<"Pr ess any key.....";
    getch();

    file.clear();
    }

    //Removes all record permanently
    void library::packre c()
    {
    //temp file
    ofstream outfile;
    outfile.open("t emp",ios::out) ;

    file.seekg(0L,i os::beg);

    while(file.read ((char*)&books, sizeof(books)))
    {
    if(books.flag!= '*')
    outfile.write(( char*)&books,si zeof(books));
    }

    outfile.close() ;
    file.close();
    remove("library ");
    rename("temp"," library");
    //file.open("libr ary.dat",ios::b inary|ios::in|i os::out|ios::no create);
    file.open("libr ary",ios::binar y|ios::in|ios:: out);
    }


    //Search by id

    void library::search id()
    {

    int tempid;
    cout<<"Enter id of book to search"<<endl;
    cin>>tempid;

    file.seekg(0L,i os::beg);

    while(file.read ((char*)&books, sizeof(books)))
    {
    if(books.id==te mpid)
    {
    cout<<endl<<"Re cord NO:";
    cout<<endl<<"ID is "<<books.id ;
    cout<<endl<<"Su bject is "<<books.subjec t;
    cout<<endl<<"Ti tle is "<<books.ti tle;
    }

    }

    cout<<endl<<"Pr ess any key...";
    getch();

    file.clear();
    }


    void library::search ttl()
    {

    string tempttl;
    cout<<"Enter title of book to search"<<endl;
    cin>>tempttl;

    file.seekg(0L,i os::beg);

    while(file.read ((char*)&books, sizeof(books)))
    {
    if(books.title= =tempttl)
    {
    cout<<endl<<"Re cord NO:";
    cout<<endl<<"ID is "<<books.id ;
    cout<<endl<<"Su bject is "<<books.subjec t;
    cout<<endl<<"Ti tle is "<<books.ti tle;
    }

    }
    cout<<endl<<"Pr ess any key...";
    getch();

    file.clear();
    }

    void library::exit()
    {
    file.close();
    }


    void main()
    {
    char choice;
    library book;

    do
    {
    //clrscr();
    cout<<endl<<"1. Add record"<<endl;
    cout<<"2.List records"<<endl;
    cout<<"3.Modify records"<<endl;
    cout<<"4.Delete records"<<endl;
    cout<<"5.Recall records"<<endl;
    cout<<"6.Pack records"<<endl;
    cout<<"7.Search by ID"<<endl;
    cout<<"8.Search by title"<<endl;
    cout<<"0.EXIT"< <endl;
    cout<<"Enter your choice"<<endl;
    cin>>choice;
    //clrscr();

    switch (choice)
    {
    case '1':
    book.addrec();
    break;

    case'2':
    book.listrec();
    break;

    case'3':
    book.modifyrec( );
    break;

    case'4':
    book.delrec();
    break;

    case'5':
    book.recallrec( );
    break;

    case'6':
    book.packrec();
    break;

    case'7':
    book.searchid() ;
    break;

    case'8':
    book.searchttl( );
    break;


    case'0':
    book.exit();
    exit(1);
    }
    }

    while (choice != 0);
    }
Working...