It's my school project, I wrote this C++ program with file handling. I am getting these errors and I can't understand what's causing the errors, if anyone can rectify my program, that would be great.
My program :-
#include<fstream.h> // for C++ and File-Handling functions
#include<conio.h> // for getch() and
#include<stdio.h> // for C functions
#include<stdlib.h> // for exit() function
// function prototypes
modData();
appData();
admin();
consumer();
addData();
showData();
menu();
searchData();
// creating class for petshop
class petShop
{
private:
char petType[31],petNickName[35],houseType[10];
int petID,quantity;
float age,price;
float totalCost(int a,float b)
{
float total;
total=a*b;
return total;
}
public:
input() // to input data from user
{
cout<<"\t\t\tEnter Pet ID :- ";
cin>>petID;
cout<<"\t\t\t\tEnter Pet Nickname :- ";
gets(petNickName);
fflush(stdin);
cout<<"\t\t\tEnter Pet Type :- ";
gets(petType);
fflush(stdin);
cout<<"\t\t\tEnter House Type for the pet :- ";
gets(houseType);
fflush(stdin);
cout<<"Enter age of the animal :- ";
cin>>age;
cout<<"Enter cost per animal :-";
cin>>price;
cout<<"Enter available animals :- ";
cin>>quantity;
}
showData() // to show data
{
cout<<"Pet ID :- "<<petID<<endl;
cout<<"Pet Nickname :- "<<petNickName<<endl;
cout<<"Pet Type :- "<<petType<<endl;
cout<<"House Type :- "<<houseType<<endl;
cout<<"Pet's Age :- "<<age;
cout<<"Available Pet's :- "<<quantity;
cout<<"Price per animal :- "<<price;
}
modifyData() // to modify data
{
cout<<"\t\t\tEnter New Nickname for the pet :- ";
gets(petNickName);
fflush(stdin);
cout<<"\t\t\tEnter New Age :- ";
cin>>age;
cout<<"\t\t\tEnter new price :- ";
cin>>price;
cout<<"\t\t\t Enter new quantitiy :- ";
cin>>quantity;
}
int check(int r) // for modData() and searchData() functions
{
if(r==petID)
{
return 1;
}
else
{
return 0;
}
}
};
int menu() // menu function
{
int ch;
main:
cout<<"Enter 1. for Administrator View"<<endl;
cout<<"Enter 2. for Consumer View"<<endl;
cout<<"Enter 3. to exit"<<endl;
cout<<"Enter your choice :- ";
cin>>ch;
switch(ch)
{
case 1: admin();
break;
case 2: consumer();
break;
case 3: exit(1);
break;
default : cout<<"Wrong choice entered, going back to main. "<<endl;
goto main;
break;
}
return 0;
getch();
}
int admin() // menu for administrator
{
int ch;
main:
cout<<"Enter 1. to add records (this will rewrite all records in backend)"<<endl;
cout<<"Enter 2. to append data"<<endl;
cout<<"Enter 3. to show all records"<<endl;
cout<<"Enter 4. to modify data"<<endl;
cout<<"Enter 5. to search for data"<<endl;
cout<<"Enter 6. to exit"<<endl;
cout<<"Enter your choice :- ";
cin>>ch;
switch(ch)
{
case 1: addData();
break;
case 2: appData();
break;
case 3: showData();
break;
case 4: modData();
break;
case 5: searchData();
break;
case 6: exit(1);
break;
default : cout<<"Wrong choice entered, going back to main menu";
goto main;
break;
}
return 0;
}
int addData() // add data function for the admin
{
petShop p;
char ch='y';
fstream f1;
f1.open("Pet_Shop.dat",ios::out|ios::in|ios::binary);
if(!f1)
{
cout<<"\n File does not exist";
exit(0);
}
while(ch=='y'||ch=='Y')
{
p.input(); //inputs data in object
f1.write((char*)&p,sizeof(p));
cout<<"\nWant to enter more data?";
cin>>ch; // y OR Y allows loop to continue
}
f1.close();
getch();
admin();
return 0;
}
int appData() // append data function for the admin
{
petShop p;
char ch='y';
fstream f1;
f1.open("Pet_Shop.dat",ios::out|ios::app|ios::binary);
if(!f1)
{
cout<<"\n File does not exist";
exit(0);
}
while(ch=='y'||ch=='Y')
{
p.input(); // appending data
f1.write((char*)&p,sizeof(p));
cout<<"\nWant to entr more data?";
cin>>ch;
}
f1.close();
getch();
admin();
return 0;
}
int showData() // output data function for the admin
{
petShop p;
fstream f1;
f1.open("Pet_Shop.dat",ios::in|ios::out|ios::binary);
if(!f1)
{
cout<<"\n File does not exist";
exit(0);
}
f1.read((char*)&p,sizeof(p)); // first object to be read before the loop
while(!f1.eof)
{
p.showData(); // display object read from file
f1.read((char*)&p,sizeof(p); //reading next object
}
f1.close();
getch();
admin();
return 0;
}
int modData() // modify data function for the admin
{
petShop p;
char ch='y';
int pID;
int rec=0; // to calculate the location of a record
cout<<"Enter Pet ID of the pet to be changed :- ";
cin>>pID;
fstream ifile;
ifile.open("Pet_Shop.dat",ios::in|ios::out|ios::binary);
if(ifile==NULL)
{
cout<<"\n File does not exist";
exit(0);
}
ifile.read((char*)&p,sizeof(p));
rec++;
while(!ifile.eof)&&ch=='y')
{
int i=petShop.check(pID);
if(i==1)
{
p.modifyData();
ifile.seekg((rec-1)*sizeof(p),ios::beg);
ifile.write((char*)&p,sizeof(p));
ch='n';
}
else
{
ifile.read((char*)&p,sizeof(p));
rec++;
}
}
ifile.close();
return 0;
getch();
}