Thread: Book Inventory (why not running?) help plz

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    Book Inventory (why not running?) help plz

    hi,
    my program of book inventory with no errors is giving errors at running time. plz check it , im writing algorithm also here:



    Code:
    ALGORITHM - BOOK INVENTORY PROGRAM C++
    
        create an inventory of books using constructor and destructor, the step by step algorithm is given below. 
    
      You have to create a class named “book” with  
    
      
      Attributes: string variable for author, title, and publisher and the integer variables for price and stock_position, successful_transaction, unsuccessful_transaction
    
      Functions:- 
      Ø  book(title, author, publishes, price and stock position) [stock position means number of books available] 
      Ø  buybook(title, author) [stock means number of  books user want to buy]
      Ø  transaction
      Ø  display.
      Ø  Int getStockPosition();
      Ø  ~book()
      
      
      Algorithm
      Step 1: Create an object for the class book.
      Step 2: Declare author, title, and publisher and the
    
      Variable for price and stock position.
      Step 3: create an array  of book objects and read 10 books from user i.e. create 10 books.
    
      Step 4: Display a menu with the following choices 
      Create books ,buybook, transaction and display.
      Step 5:Using switch case execute the statements corresponding to the choice.
    
      Step 6:If the choice is create, read the title, author, publishes, price and stock 
    
    position and pass it to the constructor of the book class.
     
      
    
      Step 7:If the choice is buy books, read the title, author from the user and
    
      check these with the array already created.
      Step 8:If the author name and title matches and stock is available then display the message”:Available”
    
      Step 9:Decrement the stock position by 1 and display the mount to be paid.
    
      Note:- if after decrement stock position becomes zero, delete that object i.e. call destructor, and destructor should display the message that stock of the sold book [book title] is ended.
      Increment successful transaction by 1. Else display “NOT success” and increment the
      unsuccessful transaction by 1.
      Step 10:If the choice is  “transaction” then display the variables, successful transaction and
    
      unsuccessful transaction.
      Step 11:If the choice is “display”, then display all the details of the books such as title, author, price,
    
      publishes and stock position.




    Code:
    /////source or main cpp file
    
    
    #include<iostream>
    #include"book.h"
    using namespace std;
    void main()
    {
    system("COLOR 17");
    
    book ob1;
    ob1.Fmenu();
    
    
    system("pause");
    
    }
    Code:
    #pragma once
    class book
    {
    /////book.h class file
    
    private:
    
        char author[40],title[40], publisher[40];
        int price, stock_position, successful_transaction, unsuccessful_transaction,stock;
        int n;
    
    public:
        
        int choice; ///variable
        void createBooks();
        book(char,char,char,int);
                                        ///book(title, author, publisher, price,stock position) 
        void buybook(char,char, int);
                                        ///buybook(title, author,stock)
        void transaction(int,book x[]);  ///flag, object
        void display(book x[]);  ////argument object of class
        int getStockPosition();
    
        void Fmenu();
    
    
        book(void);
        ~book(void);
    };

    Code:
    ////book.cpp ///cpp file of class
    
    #include "book.h"
    #include<iostream>
    //////#include<string>
    using namespace std;
    
    
    book::book(void)
    {
    }
    
    void book::Fmenu()
    {
        char name[40],aname[40];
        int price;
    cout<<"*****BOOK INVENTORY*****\n";
    cout<<"1. Create Books\n2. Buy Books\n3. Transaction\n4. Display\n";
    cout<<"Enter Choice: ";
    cin>>choice;
    switch(choice)
    {
    case 1:
        createBooks();
        break;
    case 2:
        cout<<"\nEnter Book Name: ";
        cin>>name;
        cout<<"\nEnter Author Name: ";
        cin>>aname;
        cout<<"\nHow many books you want to buy? : ";
        cin>>price;
        buybook(name[40], aname[40], price);
        break;
    case 3:
        book *ob2;
        transaction(0,&ob2[n]);
        break;
    case 4:
        display(&ob2[n]);
        break;
    }}
    
    void book::createBooks()
    {
    cout<<"How many books you want to create?";
    cin>>n;
    book *ob2=new book[n];
    for(int i=0; i<n; i++)
    {
    cout<<"Enter Book Name: ";
    cin>>ob2[i].title;
    cout<<"Enter Book Author: ";
    cin>>ob2[i].author;
    cout<<"Enter Book Publisher: ";
    cin>>ob2[i].publisher;
    cout<<"Enter Stock Position: ";
    cin>>ob2[2].stock_position;
    cout<<"Enter Book Price: ";
    cin>>ob2[i].price;
    cout<<"------------------\n\n";
    }}
    
    void book::buybook(char vtitle,char vauthor, int vstock)
    {
        book *ob2;
        for(int i=0; i<10; i++)
        {
        if(vtitle==*ob2[i].title && vauthor==*ob2[i].author)
        {
        cout<<"\nAVAILABLE !\n";
        cout<<"Total Price: "<<vstock*(ob2[i].price);
    
        transaction(1,&ob2[n]);
        ob2[i].stock_position--;
        if(ob2[i].stock_position<=0)
          delete &ob2;
    
        }
        else transaction(2, &ob2[n]);
        }    
    }
    
    void book::transaction(int flag, book ob2[])
    {
        //book *ob2;
        if(flag==1)
        {
        successful_transaction++;
        cout<<"\nSuccessful";
        }
        else if(flag==2)
        {
        for(int i=0; i<n; i++){
            cout<<"Books\tSuccessful Transaction\tUnsuccessful Transaction\n";
            cout<<"---------------------------------------------------------------------\n";
            cout<<ob2[i].title<<"\t"<<successful_transaction<<"\t"<<unsuccessful_transaction<<endl;
        }}
    
        else 
        {
        unsuccessful_transaction++;
        cout<<"\nNOT Success";
        }
    }
    
    void book::display(book ob2[])
    {
        cout<<"\nTitle\tAuthor\tPrice\tPublisher\tStock Position\n";
        for(int i=0; i<n; i++)
            cout<<ob2[i].title<<"\t"<<ob2[i].author<<"\t"<<ob2[i].price<<"\t"<<ob2[i].publisher<<"\t"<<ob2[i].stock_position<<endl;    
    }
    
    
    book::~book(void)
    {
        
        //cout<<"The stock of the sold book"<< ob2. << "is ended.";
        cout<<"\nThe stock of the sold book is ended.\n";
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    no one?

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    what are the runtime errors? - be more descriptive - and do you really need to use pragma once? Is that copied from somebody else's file and you just left it in?

    - nobody wants to assemble your code for you to discern the 'runtime error' - if you are asking something, provide the problem.
    - your code needs formatting to make it more readable
    Last edited by rogster001; 04-07-2013 at 02:35 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also never use a function to retrieve a C-string that doesn't limit the number of characters it will receive. To safely use C-strings with cin you should #include the iomanip header file and use the setw() function to limit the number of characters to be retrieved.

    Code:
    char something[20];
    cin >> setw(20) >> something;
    Better yet stop using C-strings and start using std::strings.

    Also beware that the extraction operator stops processing strings when it encounters a whitespace character, so if you really should be using getline() to retrieve strings of any kind.

    You also need to use more descriptive variable names. For instance naming a variable "n" in your class can get very confusing when you go to use that variable. Single letter names should be limited to the closest possible scope. Plus I don't see where you are actually assigning a value to this variable, it also looks like it should be a const and initialized in your constructor.


    Jim

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Replace all char arrays with std::string. Replace your new with std::vector.
    Instead of inputting data in a function in the class, it is better practice to actually pass in the info into the function via parameters and have the getting in the function that creates the object (for example, main). The code becomes more flexible then.
    You are also using pointers without initializing them.

    I am not going to diagnose your error(s). There are probably many, and I can spot one instance that is probably guaranteed to crash.
    Suffice to say, you need to modernize yourself. Read up about std::string and std::vector, then change your code to use them.
    I don't want to see any use of new. You'll probably find that your code is easier to read and probably works a ton better.
    After you've done that and you still have problems, feel free to repost your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    DO NOT PM ME AND LINK ME TO YOUR POSTING!!!

    Noobs ........ me off

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory Help
    By Fekore in forum C Programming
    Replies: 13
    Last Post: 08-02-2012, 07:16 PM
  2. Thoughts on Menu System for Book Inventory
    By curlious in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2003, 03:32 AM
  3. parsing author name for book inventory prog
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2003, 12:54 PM
  4. Book inventory program
    By curlious in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2003, 07:47 PM
  5. inventory program
    By ManicC in forum C Programming
    Replies: 5
    Last Post: 11-12-2001, 08:47 PM