Code:
#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//#include<>
using namespace std;
class item{
char name[20];
int price;
public:
char* gname()
{
return name;
}
int gprice()
{
return price;
}
void input()
{
cout<<"enter new item name : ";
cin>>name;
cout<<"enter its price : ";
cin>>price;
}
void disp()
{
cout<<"\nitem name : "<<name;
cout<<"\nprice : "<<price<<"\n";
}
void modify()
{
cout<<"enter new price((-1) to keep old price) : ";
cin>>tprice;
if(tprice!=-1)
prive=tprice;
cout<<"enter new name(. to keep old name) : ";
cin>>tname;
if(strcmp(tname,'.')!=0)
strcpy(name,tname);
}
};
class newuser{
char name[20],pass[30];
int shopno,subsc;
public:
newuser()
{
strcpy(name,"\0");
strcpy(pass,"\0");
shopno=0;
subsc=0;
}
void getdata(char user[],char pas[],int shopn,int track)
{
strcpy(name,user);
strcpy(pass,pas);
shopno=shopn;
subsc=track;
}
int getshopno()
{
return shopno;
}
int match(char user[],char pas[])
{
if(strcmp(user,name)==0&&strcmp(pas,pass)==0)
{
return 1;
}
else
{
return 0;
}
}
void copy(newuser *cur)
{
cur->shopno=shopno;
strcpy(cur->name,name);
strcpy(cur->pass,pass);
cur->subsc=subsc;
}
char* getname()
{
return name;
}
int getsub()
{
return subsc;
}
void shop(int,char);
};
void newuser :: shop(int track,char mode)
{
clrscr();
char namef[]={track+96,'.','t','x','t','\0'},name[20];
int i;
item a;
if(mode=='a')
{
a.input();
ofstream inp(namef,ios::app);
inp.write((char*)&a,sizeof(a));
cout<<"new item added!";
inp.close();
}
else if(mode=='s')
{
//display lines start---------------------------------
ifstream outp(namef);
cout<<"you have the following items :-\n";
while(!outp.eof())
{
outp.read((char*)&a,sizeof(a));
a.disp();
}
outp.close();
//end-------------------------------------------------
}
else if(mode=='d')
{
//display lines start---------------------------------
ifstream outp(namef);
cout<<"you have the following items :-\n";
while(!outp.eof())
{
outp.read((char*)&a,sizeof(a));
a.disp();
}
//end-------------------------------------------------
outp.close();
cout<<"enter the item name to be deleted : ";
cin>>name;
cout<<"item deleted!";
cout<<"\nlist after deletion :-";
ifstream outf(namef);
ofstream inp("temp.txt");
while(!outf.eof())
{
outf.read((char*)&a,sizeof(a));
if(strcmp(a.gname(),name)==0)
{
continue;
}
inp.write((char*)&a,sizeof(a));
a.disp();
}//end of while
inp.close();
outf.close();
remove(namef);
rename("temp.txt",namef);
}
else if(mode=='m')
{
item newitem;
//display lines start---------------------------------
ifstream outp(namef);
cout<<"you have the following items :-\n";
while(!outp.eof())
{
outp.read((char*)&a,sizeof(a));
a.disp();
}
outp.close();
//end-------------------------------------------------
cout<<"enter the item name to be modified : ";
cin>>name;
newitem.modify();
cout<<"item modified!";
ifstream outf(namef);
ofstream inp("temp.txt");
while(!outf.eof())
{
outf.read((char*)&a,sizeof(a));
if(strcmp(a.gname(),name)==0)
{
inp.write((char*)&newitem,sizeof(newitem));
}
else
{
inp.write((char*)&a,sizeof(a));
}
}//end of while
inp.close();
outf.close();
remove(namef);
rename("temp.txt",namef);
cout<<"list after modification :-\n";
ifstream dispf(namef);
while(!dispf.eof())
{
dispf.read((char*)&a,sizeof(a));
a.disp();
}//end of while
dispf.close();
}
else if(mode=='c')
{
remove(namef);
ofstream newf(namef);
newf.close();
cout<<"shop cleared!";
}//end if if-else...
getch();
}//end of function void shop();
int run()
{
newuser s[25],cur;
char user[20],pass[30];
int choice,i,j,track=0,subsc=0,shopno;
for(int z=0;z!=1;)
{
clrscr();
cout<<"\t\t\twelcome to the online shop\n\n\n";
if(cur.getshopno()==0)
{
cout<<"you are not registered or logged in\n";
cout<<"1)log in\n2)register\n3)exit\n";
cin>>choice;
clrscr();
switch(choice)
{
case 1:{
int check=0;
cout<<"enter user name : ";
cin>>user;
cout<<"enter password : ";
cin>>pass;
for(i=0;s[i].getshopno()!=0;i++)
{
if(s[i].match(user,pass))
{
s[i].copy(&cur);
subsc=cur.getsub();
check++;
break;
}
}
if(check==0)
{
cout<<"username or password is incorrect\n";
cout<<"getting you back to home screen...";
getch();
}
break;
}//end of case-1
case 2:{
if(track==25)
{
cout<<"this system can bear no more users\n";
cout<<"getting you back to the main screen...";
getch();
break;
}
cout<<"enter your user name : ";
cin>>user;
cout<<"enter your password : ";
cin>>pass;
cout<<"enter your shopno : ";
cin>>shopno;
s[track].getdata(user,pass,shopno,track);
s[track].copy(&cur);
track++;
subsc=track;
clrscr();
break;
}//end of case-2
case 3:{
cout<<"exitting....";
getch();
return track;
break;
}//end of case-3
}//end of switch
}//end of if(cur.shopno()==0)
else
{
cout<<cur.getname()<<"\n\n";
cout<<"what would you like to do ?\n";
cout<<"\n1)check your shop";
cout<<"\n2)add one item";
cout<<"\n3)delete one item";
cout<<"\n4)modify one item";
cout<<"\n5)clear your shop";
cout<<"\n6)log out\n";
cin>>choice;
switch(choice)
{
case 1:{
cur.shop(subsc,'s');
break;
}
case 2:{
cur.shop(subsc,'a');
break;
}
case 3:{
cur.shop(subsc,'d');
break;
}
case 4:{
cur.shop(subsc,'m');
break;
}
case 5:{
cout<<"are you sure want to clear your shop ?(y/n)";
if(getch()=='y')
{
cur.shop(subsc,'c');
}
break;
}
case 6:{
cur.getdata(user,pass,0,0);
break;
}
}
}
}//end of main loop
}//end of function int run()
char* getnamef(int pos)
{
char arr[]={pos+96,'.','t','x','t','\0'};
return arr;
}
int main()
{
clrscr();
int stat=run(),i=1;
/*removing the files before exitting*/
char arr[]={i+96,'.','t','x','t','\0'};
for(;i<=stat;i++)
{
remove(arr);
arr[0]++;
}
return stat;
}
the logical problem is serious because i am not able to find the error!!!