hi

i want to :

1. view all the books
2. search for a book

i have managed to complete part of this . the code is as follow:

#include <iostream>
#include <fstream>
#include <assert.h>
#include <stdlib.h>
using namespace std;


const int BOOK_MAX = 5000 ;
const int USER_MAX = 1400;


struct User_D
{
  char username [20];
  int houseno[3];
  char streetname [20];
  double userno;
};

struct Book_D
{
  int bookno,noc;
  char authorsurname [20];
  char bookname[50];
  char authorforename [15];
  char genre [1];
  double booknumber;

public:
  void showbook();
  void showall();
  int search3(char a[]);
  int issue(char a[]);
  int retrn(char a[]);
}L;


void Book_D::showall()
{  cout<<"\n"<<[BookD].authorsurname<<"\t\t"<<[BookD].authorforename<<"\t\t\t\t\t"<<[BookD].bookname<<"\t\t\t\t\t"<<[BookD].genre<<"\t"<<[Book\
D].booknumber<<"\n";
}


void Book_D::showbook()
{

  cout<<"\nBook No. : "<<booknumber<<"\nTitle : "<<bookname<<"\nAuthor : "<<authorforename<<authorsurname<<"\nGenre:" <<genre;
}


int Book_D::search3(char name[])
{
  int f=0;
  if(strcmp(authorsurname,authorforename)==0)
    {
      showbook();
      f=1;
    }
  else
    f=0;
  return f;
}


int Book_D::issue(char name1[])
{
  int f=0;
  if(strcmp(bookname,name1)==0) {
    noc--;
    showbook();
    cout<<"\n!!BOOK ISSUED FROM LIBRARY!!";
    f=1;
  }
  else f=0;
  return f;
}


int Book_D::retrn(char name2[])
{
  int f=0;
  if(strcmp(bookname,name2)==0) {
    noc++;
    showbook();
    cout<<"\n!!BOOK RETURNED TO LIBRARY!!";
    f=1;
  }
  else f=0;
  return f;
}





int main ()

{
  int ans;
  int i;
  int n,tot,flag=0,ch1,ch2;
  int a;
  char nm[50];
  char b[50],c[50],d[50];
  char authorname;
  char btitel;


  cout<<"\n\t\t\t1.Show Entire Library"
      <<"\n\t\t\t2.Search For a Book"
      <<"\n\t\t\t3.Issue a Book"
      <<"\n\t\t\t4.Return a Book"
      <<"\n\t\t\t5.Exit"
      <<"\n\n\t\t\t\tEnter Choice:\t";

  cin >> ans;

    switch(ans)
      {
      case'1':
        ifstream ifile;
        ifile.open("bookdata.dat");

        if (!ifile)
          {
            cerr << "File not found\n" ;
            exit (0);
          }

        Book_D hname[USER_MAX];

        cout<<"\n"<<"FIRSTNAME"<<"\t\t"<<"SURNAME"<<"\t\t\t\t\t"<<"BOOKTITLE"<<"\t\t\t\t\t"<<"GENRE"<<"\t"<<"BOOK NO.\n";

        for (int Book_D=0;Book_D<50;Book_D++)
          {
            for (int i=0; i<40;i++)
              {
                ifile.get(hname[Book_D].authorsurname[i]);
                cout<<hname[Book_D].authorsurname[i];
              }
          }cout << "\n";

        ifile.close();
        break;



      case2:

        cout<<"\n\n\n\n\n\n\n\t\t\tEnter Author name:";
        cin>> authorname;

        ifstream ifile2;
        ifile2.open("bookdata.dat");

        ifile2.read((char*)&L,sizeof(L));
        while(ifile2)
          {
            flag=L.search3(b);
            if(flag==1)
              {
                ifile2.close();
                break;
              }

            ifile2.read((char*)&L,sizeof(L));
          }
        if(flag==0) cout<<"\n\n\n\t\t\t!!NO BOOKS WRITTEN BY THIS AUTHOR!!";
        ifile2.close();
        break;

      case3:
        cout<<"\n\n\n\n\n\n\n\t\t\tEnter Title:";
        cin>> btitle;

        ifstream iofile;
       iofile.open("bookdata.dat",ios::in|ios:ut,ios::binary);

        iofile.read((char*)&L,sizeof(L));
        while(iofile)
          {
            flag=L.issue(c);
            if(flag==1)
            {
              iofile.seekp(iofile.tellg()-sizeof(L));
              iofile.write((char*)&L,sizeof(L));
              break;
            }
            iofile.read((char*)&L,sizeof(L));
          }
        if(flag==0) cout<<"\n!!NOT FOUND!!";
        iofile.close();
    
        break;
      }
}

can you tell me what i am doing wrong? thank you.
:cry:

Recommended Answers

All 5 Replies

can you tell me what i am doing wrong? thank you.

Can't say until you tell us what problems you are having. compiler errors? yes, what are they. runtime errors? name them too. don't be afraid to elaborate a little bit -- band width is not very expensive :)

For one thing, this:
void Book_D::showall()
{ cout<<"\n"<<[BookD].authorsurname<<"\t\t"<<[BookD].authorforename<<"\t\t\t\t\t"<<[BookD].bookname<<"\t\t\t\t\t"<<[BookD].genre<<"\t"<<[Book\
D].booknumber<<"\n";
}

makes no sense whatsoever - what's [BookD]. ?

I don't know what [BookD].authorsurname is either -- not a valid statement. What are you trying to do with that line? Did YOU write that code? Apparently not if you have to ask what it means.

My guess is that the function is displaying the values of one instance of the class. Somewhere else is an array of classes.

void Book_D::showall()
{  cout<<"\n"<<authorsurname<<"\t\t"<<authorforename<<"\t\t\t\t\t"<<bookname<<
               << "\t\t\t\t\t"<<.genre<<"\t"<<booknumber<<"\n";
}
int main()
{
     Book_D array[80; // an array of 80 Book_D objects
   ...
     for(int i = 0; i < 80; i++)
          array[i].showall();
   ...
}

I think you need to write out the design of your program in your native language before you try to write up the program to do what you want to do. Expanding on this may work:

A library is a collection of books. The information regarding each book is contained in a database which will be represented by a container that I know how to use (may be an array, vector, list, whatever). Each book has an authors name, a title, and an id number. In addition, each book has the the status of being either available or checked out. If the book is checked out, then it has a non-default checked out date and a non-default return by date. Each date has a month, day, and year field. All this data is encapsulated in a class/struct paradigm. The authors name, book title, and id number are initially located in a file and need to be read into the program's database before the database is searchable. Each book can be searched for by author, title, id number, (and whatever other information you want to include in the Book class).

When you declare variables within a switch statement such as the hname array in switch case 1, you need to use {} to enclose the body of the switch case. The scope of the variable will be limited to that set of {} so if you want to use that variable elsewhere in the program you should declare it with braoder scope.

You'll want <cstring> for strcmp().

I think [BookD].authorsurname is either Objective C or an attempt to do this:

(*BookD).authorsurname

[edit]
Or maybe it's Java, judging by his avatar. :)
[/edit]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.