i have written a program in c++ using the concept of file handling. this program is about managing a basic electronic hardware store stock and showing the sales report. i am not able to understand why the contents of my file are not being displayed, modified, deleted, or even printing the bill or sales report... though i have tried to find answers to my problems in the previous threads i have not achieved much.. please read the code and suggest the necessary editing.. your help will be highly appreciated..
#include<iostream>
#include<fstream>
//#include<conio>
#include<string.h>
#include<iomanip>
#include<cstdio>
using namespace std;
class eshop
{
int pdtcode;
char name[20];
char model[10];
float price;
float discount;
char description[100];
int iqty;
public:
int qty;
void getdata(void)
{
cout << "\n enter name of product :" ;
cin.ignore(10,'\n');
cin.getline(name,19);
cout << "\n enter product code : " ;
cin >> pdtcode;
cout << "\n enter name of model : " ;
cin.ignore(10,'\n');
cin.getline(model,9);
cout << "\n enter price : " ;
cin >> price;
cout << "\n enter product description : " ;
cin.ignore(10,'\n');
cin.getline(description,60);
cout << "enter quantity of the product : ";
cin >> iqty;
qty = iqty;
}
void showdata()
{
cout << "\n product code : " << pdtcode;
cout << "\n name : " << name;
cout << "\n model : " << model;
cout << "\n quantity: " << qty;
cout << "\n description : " << description;
cout << "\n M.R.P : " << price;
cout << "\n discount : " << discount;
cout << "\n final price : " << price - discount;
}
void calcdiscount()
{
if (price >= 2000.00 && price < 5000.00)
discount = 0.05*price;
if (price >= 5000.00 && price < 10000.00)
discount = 0.10*price;
if (price >= 10000.00)
discount = 0.20*price;
else discount = 0.00;
}
int retpdtcode()
{ return pdtcode; }
};
void main()
{
eshop obj;
fstream file , copyfile;
//file.open("eshopfile.dat", ios::in | ios::app | ios::binary);
//copyfile.open("eshop_copy.dat", ios::app | ios::in | ios::out | ios::binary | ios::trunc);
int x,code,pos,var,found=0,dele=0;
char choice = 'y';
do
{
cout << "\n 1. Display current stock : " ;
cout << "\n 2. Add an item : " ;
cout << "\n 3. Modify the stock : " ;
cout << "\n 4. Delete an item : " ;
cout << "\n 5. Make a bill for the purchase" ;
cout << "\n 6. Display product report: " ;
cout << "\n 7. Quit : " ;
cout << "\n" ;
cout << "\n Enter your option : " ;
cin >> x;
switch (x)
{
case 1:
file.open("eshopfile.dat", ios::binary );
while (file.read((char*)&obj,sizeof(obj)))
obj.showdata();
file.close();
break;
case 2:
file.open("eshopfile.dat", ios::binary | ios::app );
obj.getdata();
file.write((char*)&obj,sizeof(obj));
while (choice == 'y' || choice == 'Y' )
{
cout << "do you want to add more items? y/n";
cin >> choice;
if ( choice == 'y' || choice == 'Y' )
{
obj.getdata();
file.write((char*)&obj,sizeof(obj));
}
}
file.close();
break;
case 3:
file.open("eshopfile.dat" , ios::binary | ios::in );
cout << "\n enter product code of item to be edited : ";
cin >> code;
file.seekg(0);
file.seekp(0);
while (!found && file.read((char*)&obj,sizeof(obj)))
{ if (obj.retpdtcode()==code)
found++;
if(found)
{ cout << "\n Enter details again... ";
obj.getdata();
pos = (int)file.tellg() - sizeof(obj);
file.seekp(pos);
file.write((char*)&obj,sizeof(obj));
}
else
cout << "\n no record matching for editing";
}
file.close();
break;
case 4:
file.open("eshopfile.dat" , ios::binary | ios::in );
copyfile.open("eshop_copy.dat" , ios::binary | ios::out);
cout << "\n enter product code of item to be deleted : ";
cin >> code;
file.seekg(0);
copyfile.seekp(0);
while(file.read((char*)&obj,sizeof(obj)))
{
if (obj.retpdtcode()!=code)
copyfile.write((char*)&obj,sizeof(obj));
else
dele++;
}
if (!dele)
cout << "\n record match not found... " ;
else
cout << "\n record deleted... " ;
file.close();
copyfile.close();
var = rename ( "eshopfile.dat", "eshop_copy.dat" );
file.close();
copyfile.close();
break;
case 5:
file.open("eshopfile.dat", ios::binary );
cout << "\n enter product code of item being purchased : ";
cin >> code;
file.seekg(0);
found = 0;
while(file.read((char*)&obj,sizeof(obj)))
{
if (obj.retpdtcode()==code)
found++;
if(found)
{
obj.showdata();
obj.qty-=1;
pos = (int)file.tellg() - sizeof(obj);
file.seekp(pos);
file.write((char*)&obj,sizeof(obj));
}
}
file.close();
break;
case 6:
cout << "...........PRODUCT SALES REPORT...........\n" ;
file.open("eshopfile.dat", ios::binary );
file.seekg(0);
while(file.read((char*)&obj,sizeof(obj)))
{
obj.showdata();
}
file.close();
break;
case 7:
break;
default:
cout << "wrong choice... ";
}
cout << " ";
}while(x>0&& x < 7 && x!= 7);
}
i am using the visual c++ 2010 version..