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();
}

Errors I am getting are :-

--------------------Configuration: petShop - Win32 Debug--------------------
Compiling...
petShop.cpp
C:\Users\Dhruv\C++ Project\petShop.cpp(48) : warning C4183: 'input': member function definition looks like a ctor, but name does not match enclosing class
C:\Users\Dhruv\C++ Project\petShop.cpp(58) : warning C4183: 'showData': member function definition looks like a ctor, but name does not match enclosing class
C:\Users\Dhruv\C++ Project\petShop.cpp(70) : warning C4183: 'modifyData': member function definition looks like a ctor, but name does not match enclosing class
C:\Users\Dhruv\C++ Project\petShop.cpp(196) : error C2276: '!' : illegal operation on bound member function expression
C:\Users\Dhruv\C++ Project\petShop.cpp(196) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

petShop.obj - 2 error(s), 3 warning(s)

you have not defined the return types of several of your member functions making the compiler think they are incorrectly named constructors.

What compiler are you using? Generally it is not a good idea to mix C and C++ code together in the same program.

What compiler are you using? Generally it is not a good idea to mix C and C++ code together in the same program.

I am using Visual C++ 6.0

I tested the code using Visual C++ 2010.
Now I got this error :-

1>------ Rebuild All started: Project: School Project, Configuration: Debug Win32 ------
1>  petShop.cpp
1>c:\users\dhruv\documents\visual studio 2010\projects\school project\school project\petshop.cpp(1): fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Visual Studio is a great tool, but when it comes to school projects (especially CBSE board), Visual Studio sucks.

That is because fstream.h (and all other C++ headers ending .h) is a pre-standardisation header i.e. over a decade old and has been depricated for sometime and I expect MS has taken action and removed it from their IDE/compiler.

use #include <fstream>

That is because fstream.h (and all other C++ headers ending .h) is a pre-standardisation header i.e. over a decade old and has been depricated for sometime and I expect MS has taken action and removed it from their IDE/compiler.

use #include <fstream>

I used fstream instead of fstream.h, after that it's not even recognizing cin and cout commands.

It doesn't seem as if you've included <iostream> for cin and cout. Even after including that you will also need to qualify the names. You can do this one of three ways.
First, qualify as std::cin and std::cout at each use of cin/cout, or alternatively use the statement using namespace std; at the top of your code, or lastly (as a compromise) you could put using std::cout; and using std::cin; at the top just to qualify those two names only.

Also, these are a bad idea:

gets()  //will not limit the number of characters you can enter, you can go way over the end of your char arrays into other memory
fflush(stdin); //undefined behavior, meant to be used with stdout
goto //reform your code into loops if you want this behavior

It doesn't seem as if you've included <iostream> for cin and cout. Even after including that you will also need to qualify the names. You can do this one of three ways.
First, qualify as std::cin and std::cout at each use of cin/cout, or alternatively use the statement using namespace std; at the top of your code, or lastly (as a compromise) you could put using std::cout; and using std::cin; at the top just to qualify those two names only.

Also, these are a bad idea:

gets()  //will not limit the number of characters you can enter, you can go way over the end of your char arrays into other memory
fflush(stdin); //undefined behavior, meant to be used with stdout
goto //reform your code into loops if you want this behavior

I am using fstream, so it should include cin and cout. What I learn is that fstream is inherited from iostream,ifstream,ofstream and some other header files so includes the functions of iostream as well.

I took note of your suggestions, thanks for it. I took note of it.

That may be compiler implementation dependent. On VC++ 2010 Express fstream only includes istream. If you want cout then your program must include <ifstream>

I am using fstream, so it should include cin and cout. What I learn is that fstream is inherited from iostream,ifstream,ofstream and some other header files so includes the functions of iostream as well.

I took note of your suggestions, thanks for it. I took note of it.

No fstream in <fstream> inherits iostream in <istream>. cin in <iostream> inherits istream in <istream> and cout in <iostream> inherits ostream in <ostream>.

There is no connection in inheritance or filewise between fstream and cin or cout. To use cin or cout you must specifically include iostream.

NOTE: the class iostream is not declared in the header <iostream>

See here

commented: Excellent clarification +4
commented: Nice. +1

No fstream in <fstream> inherits iostream in <istream>. cin in <iostream> inherits istream in <istream> and cout in <iostream> inherits ostream in <ostream>.

There is no connection in inheritance or filewise between fstream and cin or cout. To use cin or cout you must specifically include iostream.

NOTE: the class iostream is not declared in the header <iostream>

See here

So, I would have to include <iostream> to use cin and cout besides <fstream> to read files!
But in Turbo C++ (which runs on my school computer), we only need to include fstream.

That's because your compiler is ancient and uses obsolete header files. It's unfortunate you and your peers are forced to use such antiquated compilers.

That's because your compiler is ancient and uses obsolete header files. It's unfortunate you and your peers are forced to use such antiquated compilers.

The school, the students want to use the latest compilers and tools, but the education board (CBSE) here in India recommends only Turbo C++ and all the course for C++ is written in a way that we are stuck up with Turbo C++. If we write the Visual Studio way in our exams, we'll score nothing.

I managed to clear all errors except one.

The new code is :-

#include<fstream> // for C++ and File-Handling functions
#include<iostream>
#include<stdio.h>  // for C functions
#include<stdlib.h> // for exit() function
using namespace std;
// function prototypes
int modData();
int appData();
int admin();
int consumer();
int addData();
int showData();
int menu();
int 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:
	int input()    // to input data from user
	{
	    cout<<"\t\t\tEnter Pet ID :- ";
        cin>>petID;
	    cout<<"\t\t\t\tEnter Pet Nickname :- ";
	    cin.getline(petNickName,35);
	    cout<<"\t\t\tEnter Pet Type :- ";
	    cin.getline(petType,31);
	    cout<<"\t\t\tEnter House Type for the pet :- ";
	    cin.getline(houseType,10);
	    cout<<"Enter age of the animal :- ";
	    cin>>age;
	    cout<<"Enter cost per animal :-";
	    cin>>price;
	    cout<<"Enter available animals :- ";
	    cin>>quantity;
		return 0;
	}
	int 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;
		return 0;
	}
	int modifyData()     // to modify data
	{
		cout<<"\t\t\tEnter New Nickname for the pet :- ";
	    cin.getline(petNickName,35);
	    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;
		return 0;
	}
	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;
}
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();
	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();
	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();
	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;
}

The errors I am getting are :-

1>------ Build started: Project: School Project, Configuration: Debug Win32 ------
1>  petShop.cpp
1>c:\users\dhruv\documents\visual studio 2010\projects\school project\school project\petshop.cpp(193): error C2276: '!' : illegal operation on bound member function expression
1>c:\users\dhruv\documents\visual studio 2010\projects\school project\school project\petshop.cpp(193): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Compiled through Visual C++ Express 2010

f1.eof() is a method and needs the parentheses. I know with text files that using .eof() can read the last line of the file twice, but I'm not sure how it behaves with binary.

You're missing a parens on 196 also.

Look at line 221 again, check() would have to be a static method for that to work.

f1.eof() is a method and needs the parentheses. I know with text files that using .eof() can read the last line of the file twice, but I'm not sure how it behaves with binary.

You're missing a parens on 196 also.

Look at line 221 again, check() would have to be a static method for that to work.

Thanks for the help. Problem solved.
Some admin should mark this thread as solved.

Since you started this thread you should be able to mark it solved yourself.

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.