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..

Your code is not formatted. Delete extra blank lines
Line 3. Is that a comment or a mistake?

i use a visual c++ 2010 version.. conio was not working, the compiler generated an error when i compiled the code with it.. so i commented it..

That's because it's conio.h. But keep it commented out. You shouldn't be using it anyway -- it's not a standard header and if you get used to using it, when you change compilers you'll have problems because all your conio.h functions will no longer exist and you'll have to weed out all those functions from your program.

well since no real help came.. i researched a bit and stumbled upon the solution.. i consulted a very old thread and found out that c++ streams have lot of junk and that in my program the file pointer did not return to the zero position or the beginning of the file and so the search and write functions were not performed properly..

in order to solve this problem i added

file.seekg(0);
file.clear();

commands at the beginning and end of all switch cases before i closed the file so that when the case changed the file pointer could start afresh..
i made these changes mainly (and a few others here and there so that the code would become more presentable).. and the program is working properly..
here is a snippet of one of the cases to show how my problem was solved..

//to modify an item in the file
case 3: 
file.open("eshopfile.dat" , ios::binary | ios::in |ios::out |ios::ate);
file.seekg(0);
file.clear();
//the next statement would show the position of read pointer
//whenever modify i.e. case 3 is called.. 
cout << "\n file.seekg() value " << file.tellg();
cout << "\n enter product code of item to be edited : ";
cin >> code;
found = 0;
while (!found && file.read((char*)&obj,sizeof(obj)))
{ if (obj.retpdtcode()==code)
   {found++;
    pos = (int)file.tellg() - sizeof(obj);
    obj.showdata();
	file.seekg(0);
    }
}
cout << "\n" << found << "\n";
file.clear();
if (found)
{  
	file.seekp(pos,ios::beg);
	cout << "\n Enter details again... ";
    obj.getdata();
    file.write((char*)&obj,sizeof(obj));
    file.seekg(0);
    file.clear();     
    file.close();
}
else 
{cout << "\n no record matching for editing";
file.seekg(0);
file.clear();
file.close();}
break;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.