got problem with this code ...

This is a discussion on got problem with this code ... within the C++ Programming forums, part of the General Programming Boards category; i write this class for holding information of books. but it doesn't work, and i cannot find where the problem ...

  1. #1
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149

    got problem with this code ...

    i write this class for holding information of books.
    but it doesn't work, and i cannot find where the problem is.

    i hope someone can help me fix it up.
    any suggestion is great~~

    Code:
    //class to store info of books and  basic method
    #include<string>
    #include<sstream>
    #include<stdlib>
    using namespace std;
    
    const long MAX_C =100000;
    const int MAX_Q =10000;
    
    class BMSBook
    {
    public:
    
    BMSBook()
    {
        isbn="";
        title="";
        press="";
        instruction="";
        type="";
        quantity=0;
        next=NULL;
    }
    BMSBook(string _isbn,string _title,string _press,string _instruction,string _type,int _quantity,BMSBook *_next)
    {
        isbn=_isbn;
        title=_title;
        press=_press;
        instruction=_instruction;
        type=_type;
        quantity=_quantity;
        next=_next;
    }
    BMSBook(BMSBook *_book)
    {
        isbn=_book->isbn;
        title=_book->title;
        press=_book->press;
        instruction=_book->instruction;
        type=_book->type;
        quantity=_book->quantity;
        next=_book->next;
    }
    
    void getdata(string &_isbn,string &_title,string &_press,string &_instruction,string &_type,int &_quantity)
    {
        _isbn=isbn;
        _title=title;
        _press=press;
        _instruction=instruction;
        _type=type;
        _quantity=quantity;
    }
    
    protected:
    string isbn;
    string title;
    string press;
    string instruction;
    string type;
    int quantity;
    BMSBook *next;
    friend class BMSBooks;
    };
    
    
    
    class BMSBooks
    {
    public:
    BMSBooks()
    {
        thebook=NULL;
        count=0;
    }
    ~BMSBooks()
    {
        while(thebook!=NULL)
        {
            BMSBook *temp;
            temp=thebook;
            thebook=temp->next;
            delete temp;
        }
    }
    
    
    void moveforward(BMSBook *find,int index)
    {
        while(index&&(find->next!=NULL))
        {
            find=find->next;
            index--;
        }
    }
    
    void searchbyisbn(BMSBook *find,int &cmp,const string &isbn)
    {
    BMSBook *temp;
    temp=thebook;
    find=thebook;
    long tempindex=1;
    long top=count;
    long bottom=1;
            moveforward(find,(top+bottom)/2-1);
            cmp=strcmp(isbn.c_str(),find->isbn.c_str());
        do
        {
            if(!cmp)
              {
                return;
              }
            else if(cmp>0)
              {
                tempindex=(top+bottom)/2;
                temp=find;
                bottom=tempindex+1;
                moveforward(find,(top+bottom)/2-tempindex);
              }
            else if(cmp<0)
              {
                find=temp;
                top=(top+bottom)/2;
                moveforward(find,(top+bottom)/2-tempindex);
              }
             cmp=strcmp(isbn.c_str(),find->isbn.c_str());
        }while(top>bottom);
    }
    
    
    
    short add(string isbn,string title,string press,string instruction,string type,int quantity)
    {
    if((count>=MAX_C)||(quantity>=MAX_Q))return 2;
    if(checkisbn(isbn))
        {
            BMSBook *find;
            BMSBook *temp;
            int cmp;
            if(count)
                {
                    searchbyisbn(find,cmp,isbn);
                    if(!cmp)
                    {
                        if((find->quantity+quantity)>MAX_Q)return 2;
                        find->quantity+=quantity;
                        return 0;
                    }
                    else if(cmp>0)
                    {
                        temp=find->next;
                        find->next=new BMSBook(isbn, title, press, instruction, type,quantity,temp);
                        if(find->next==NULL)return 1;
                        count++;
                        return 0;
                    }
                    else if(cmp<0)
                    {
                        temp=new BMSBook(find->isbn,find->title,find->press,find->instruction,find->type,find->quantity,find->next);
                        if(temp==NULL)return 1;
                        find->isbn=isbn;
                        find->title=title;
                        find->press=press;
                        find->type=type;
                        find->instruction=instruction;
                        find->quantity=quantity;
                        find->next=temp;
                        count++;
                        return 0;
                    }
                    else
                    {return -2;}
                }
            else
            {
                thebook=new BMSBook(isbn,title,press,instruction,type,quantity,NULL);
                if(thebook==NULL)return 1;
                count=1;
                return 0;
            }
        }
    return -1;
    }
    
    
    bool remove(string isbn,int quantity)
    {
        if(count)
        {
            BMSBook *find;
            BMSBook *temp;
            int cmp;
            searchbyisbn(find,cmp,isbn);
            if(!cmp)
                {
                    if(find->quantity>quantity)
                      {
                        find->quantity-=quantity;
                      }
                    else
                      {
                        temp=find->next;
                        find->isbn=temp->isbn;
                        find->title=temp->title;
                        find->press=temp->press;
                        find->instruction=temp->instruction;
                        find->type=temp->type;
                        find->quantity=temp->quantity;
                        find->next=temp->next;
                        delete temp;
                        count--;
                      }
                }
        }
    return false;
    }
    
    
    bool checkisbn(const string &isbn)
    {
    int sum=0,num=0,num1=0;
    if(isbn.size()!=13)return false;
    for(int loops=0;loops<12;loops++)
      {
      if(isdigit(isbn[loops]))
      {
      istringstream temp(isbn.substr(loops,1));
      temp>>num;
      sum+=(num*(++num1));
      }
      }
    num=sum%11;
    if(num>10)
    {
    if(isbn.substr(12,1)!="x")return false;
    }
    else
    {
    if(!isdigit(isbn[12]))return false;
    istringstream temp(isbn.substr(12,1));
    temp>>sum;
    if(num!=sum)return false;
    }
    return true;
    }
    
    void movetotop(BMSBook *current)
    {
    current=thebook;
    }
    
    void getcount(long &num)
    {
    num=count;
    }
    
    private:
    long count;
    BMSBook *thebook;
    };

    blow me ... ...

  2. #2
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    it is my first c++ homework, i must finish it before monday

    blow me ... ...

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >#include<stdlib>
    Change this to
    Code:
    #include<cstdlib>
    Now it compiles. If it still doesn't work, you'll need to be more specific as to how it doesn't work. What are you expecting as opposed to what it actually does? Posting a relatively large chunk of code and saying "it doesn't work" is not conducive to getting quick help around here.
    My best code is written with the delete key.

  4. #4
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    i have post the entire header file of my "BMSBooks" class. all this code do, is to make a dynamic list to hold information of books.

    i think the problem is that i cannot add new item(book),
    the add() or searchbyisbn() doesn't do the correct thing.i used binary search in searchbyisbn(), i wonder if my algorithm is right...

    blow me ... ...

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Well, in add you don't ever assign a book to find, so you're working with an uninitialized pointer. But why does searchbyisbn use a binary search algorithm on a linked list? A sequential search would have similar performance properties, and a binary search tree would be a much better choice of data structure. That is, assuming you don't want to use the standard C++ containers.
    My best code is written with the delete key.

  6. #6
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Code:
    void b(char *c)
    {
    c="hoooo";
    }
    
    void a()
    {
    char *c;
    b(a);
    }
    after running a(), the pointer c in a() wouldn't point to "hoooo"?

    blow me ... ...

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >after running a(), the pointer c in a() wouldn't point to "hoooo"?
    No. Unless you pass by reference, everything in C++ is passed by value. When you pass c to b, a copy of the pointer is made. Any assignments to that pointer will only affect the copy, not the original. If you want to re-assign c within b then you need to either pass a reference:
    Code:
    void b(char*& c)
    {
      c = "hoooo";
    }
    Or a pointer to the pointer so that you can simulate passing by reference:
    Code:
    void b(char **c)
    {
      *c = "hoooo";
    }
    My best code is written with the delete key.

  8. #8
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    you are Christ, Prelude. thanks~~~~
    without your help , it is sure that my c++ teacher will shoot me on monday.

    last question:
    a binary search tree would be a much better choice of data structure.
    i don't understand this, where should binary search be used?

    blow me ... ...

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >where should binary search be used?
    Binary search is inappropriate for a linked list because you still have to traverse the entire list to find the item. Because of this, binary search on a linked list isn't a great deal more efficient than sequential search. If you toss the linked list altogether and replace it with a binary search tree, you get binary search as well as sorted data without any extra effort.
    My best code is written with the delete key.

  10. #10
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Quote Originally Posted by Prelude
    >where should binary search be used?
    Binary search is inappropriate for a linked list because you still have to traverse the entire list to find the item. Because of this, binary search on a linked list isn't a great deal more efficient than sequential search. If you toss the linked list altogether and replace it with a binary search tree, you get binary search as well as sorted data without any extra effort.
    I have download your binary search tree tutorials

    blow me ... ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21