Function 5:
The Display Sales History Function.. It is just displaying the last entry twice...
Function 1:
The 'Add Book' Feature.. It was actually working alright until I added the 'Edit Book' and 'Add Sales' Features. Now it writes something to the file("Books.txt"); that none of the other functions identify. Earlier,i.e.
when 'Edit Book' and 'Add Sales' weren't coded and structure Sale not declared,
'Search Book' could work properly with the entry done by 'Add Book'.
((This is what I found in BOOKS.txt when I only coded functions 1 and 2 and did a test run: {{Mein Kampf ÿu)<èÿX 0@ Ÿ0 :òÿ2 XThe Accidental Prime Minister Ÿ0 :òÿ< ^Quantico ental Prime Minister Ÿ0 :òÿ ÂSurely You Are Joking, Mr. Feynman X0- ô}}
If you paste this in Books.txt, all features except 'Add' work allright))
#include <fstream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
struct Sale {
char costumer[40], name[40];
int quant, pric, netprice;
};
class Book {
public:
char name[40];
int qty, code, price;
void Add()
{
cout << "\n Enter Name Of New Book\n";
gets(name);
cout << "\n Enter book code:";
cin >> code;
cout << "\n Enter book price:";
cin >> price;
cout << "\n Enter book quantity:";
cin >> qty;
}
void Display()
{
cout << "\n Name:";
puts(name);
cout << "Code:" << code;
cout << "\nPrice:" << price;
cout << "\nQuantity Available:" << qty;
}
void Modify();
Sale Sell(int q)
{
Sale sale;
sale.pric = price;
qty -= q;
sale.quant = q;
sale.netprice = q * price;
cout << "\n Enter costumer name\n";
gets(sale.costumer);
strcpy(sale.name, name);
return sale;
}
};
void Book::Modify()
{
Display();
char ch = 'n';
do {
cout << "\n Enter detail to modify \n";
cout << "1.NAME\t2.QUANTITY\t3.CODE\t4.PRICE\n";
int mod;
cin >> mod;
switch (mod) {
case 1:
cout << "\nEnter new Name\n";
gets(name);
break;
case 2:
cout << "\nEnter new available quantity\n";
cin >> qty;
break;
case 3:
cout << "\nEnter changed CODE\n";
cin >> code;
break;
case 4:
cout << "\nEnter updated PRICE\n";
cin >> price;
break;
}
Display();
cout << "\nWant to edit more? (y/n)\n";
ch = getch();
} while (ch == 'y' || ch == 'Y');
}
void Print(Sale s)
{
cout << "\nCostumer Name:\t";
puts(s.costumer);
cout << "Book Name:\t";
puts(s.name);
cout << "\nQuantity(units):" << s.quant << "\tPrice per pice:" << s.pric << "\n\t Net Sale:" << s.netprice;
}
void MScreen()
{
clrscr();
cout << "\tTHE BOOK STORE MANAGEMENT SOFTWARE\n";
cout << "\tPLEASE CHOOSE AN OPTION";
cout << "\n1.ADD NEW BOOK\n2. SEARCH FOR A BOOK\n3. EDIT BOOK DETAILS\n 4.ADD NEW SALES\n5. DISPLAY SALES HISTORY\n";
}
void main()
{
char ch = 'n';
while (ch == 'n' || ch == 'N') {
MScreen();
int i;
cin >> i;
clrscr();
switch (i) {
case 1:
Book a;
a.Add();
ofstream obj1("BOOKS.txt", ios::app);
obj1.write((char*)&a, sizeof(Book));
obj1.close();
break;
case 2:
Book b;
ifstream obj2("BOOKS.txt");
obj2.seekg(0);
cout << "Enter Book Code:";
long int x;
cin >> x;
while (!obj2.eof()) {
obj2.read((char*)&b, sizeof(Book));
if (b.code == x) {
b.Display();
obj2.close();
break;
}
}
break;
case 3:
Book c;
cout << "\n Enter Code of book to be modified\t";
int y;
cin >> y;
fstream obj3("BOOKS.txt", ios::in | ios::out | ios::ate);
long int pos;
obj3.seekg(0);
while (!obj3.eof()) {
pos = obj3.tellg();
obj3.read((char*)&c, sizeof(Book));
if (c.code == y) {
c.Modify();
obj3.seekp(pos);
obj3.write((char*)&c, sizeof(Book));
break;
}
}
cout << "\nDATA MODIFIED\n";
break;
case 4:
char ch1 = 'n';
do {
Book d;
cout << "\n Enter the book code \t";
int z;
cin >> z;
fstream obj4("BOOKS.txt", ios::in | ios::out | ios::ate);
long int pos;
obj4.seekg(0);
int found = 0;
while (!obj4.eof()) {
pos = obj4.tellg();
obj4.read((char*)&d, sizeof(Book));
if (d.code == z) {
found = 1;
break;
}
}
if (found == 0) {
cout << "\nIncorrect Code\t Aborting";
break;
}
else {
d.Display();
cout << "\nEnter Sales Quantity:\t";
int q;
cin >> q;
if (q > d.qty) {
cout << "\n Can't Sell. Aborting ";
break;
}
else {
Sale newsale;
newsale = d.Sell(q);
obj4.seekp(pos);
obj4.write((char*)&d, sizeof(Book));
obj4.close();
ofstream obj5("Sales.txt", ios::app);
obj5.write((char*)&newsale, sizeof(newsale));
obj5.close();
cout << "\nSALE SUCCESSFUL\n";
sleep(2);
}
}
cout << "\n Add more sale? (y/n)";
cin >> ch1;
} while (ch1 == 'y' || ch1 == 'Y');
break;
case 5:
cout << "\nPlease enter SALE password:";
char pass[40];
gets(pass);
if (strcmp(pass, "creationbydhruvarora\n")) {
clrscr();
ifstream obj6("Sales.txt");
obj6.seekg(0);
Sale readsale;
int net = 0;
while (!obj6.eof()) {
obj6.read((char*)&readsale, sizeof(Sale));
net += readsale.netprice;
Print(readsale);
sleep(1);
}
cout << "\n END OF SALES\n TODAY NET SALE:" << net;
break;
}
else {
cout << "\nIncorrect Password\n ABORTING";
break;
}
}
getch();
clrscr();
cout << "Press Any Key To QUIT. To Go To Main Menu Press 'n'\n";
ch = getch();
}
}
I do not understand what this is. A piece of code added to some code is disrupting working w/o being executed.