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";
}