hey guys...!! i really want ur help in this program..!!
its a Hospital management system program..!!
it take input from the use..it can save input in a file..display all input on to screen, but i cant find a way to delete a record/patient details from the file
PLZ help me..!!
here is the coding!!
im working on C++, visual studio 2008,
everythign is working but i want help in case 3, and case 4.!!

#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;
class hospital {
	char name[80];
	char id[12];
	char telephone[7];
	int numday;
	char roomnum[3];
public:
	hospital(){ };
	hospital(char*n,char*i, char*t, char*r, int nu)
	{
		strcpy_s(name,n);
		strcpy_s(id,i);
		strcpy_s(telephone,t);
		nu=numday;
		strcpy_s(roomnum,r);
		;
	}
	friend ostream &operator<<(ostream &stream, hospital o);
	friend istream &operator>>(istream &stream, hospital &o);

};
ostream &operator<<(ostream &stream, hospital o)
{
	stream<<o.name<<"\n";
	stream<<"-"<<o.id<<"\n";
	stream<<"0213-"<<o.telephone<<"\n";
	stream<<"number of Days in hospital:"<<o.numday<<"\n";
	stream<<"Cost of stay: Rs."<<o.numday*2000<<"\n";
	stream<<"Room Number"<<o.roomnum<<"\n";
	return stream;
}
istream &operator>>(istream &stream, hospital &o)
{
	cout<<"enter name: ";
	stream>>o.name;
	cout<<"Enter ID: ";
	stream>>o.id;
	cout<<" Enter telephone number :";
	stream>>o.telephone;
	cout<<" Allocate a Room number for the Patient :";
	stream>>o.roomnum;
	cout<<" Enter number of Days spent in Hospital :";
	stream>>o.numday;
	cout<<"\n";
	return stream;
}
int main()
{
	hospital a,d;
	char c;
	fstream hp("hospital",ios::in|ios::out|ios::app);
	if(!hp){
		cout<<"cant open Hospital file.\n";
		return 1;
	}
	for(;;)
	{
		do{
			cout<<"Welcome to NED's Hospital management"<<"\n";
			cout<<"1. Enter patients details"<<"\n";
			cout<<"2. Display patient details"<<"\n";
			cout<<"3. Search a patient by name"<<"\n";
			cout<<"4. Delete a patient record:"<<"\n";
			cout<<"5.Quit"<<"\n";
			cout<<"\n Enter a choice : ";
			cin>>c;
		}while( c<'1'||c>'5');
		switch(c){
case'1':
	cin>>a;
	cout<<"entry is :";
	cout<<a;
	hp<<a;
	break;
case'2':
	char ch;
	hp.seekg(0, ios::beg);
	while(!hp.eof()){
		hp.get(ch);
		if(!hp.eof()) cout<<ch;
	}
	hp.clear();//reset eof
		cout<<endl;
		break;
case'3':

	break;

case'5':
			hp.close();
			return 0;
		}
}
}

Recommended Answers

All 9 Replies

plz help me guys!!!

The only way to physically delete a record from the file is to rewrite the entire file. If the file is small enough then just read the entire thing into memory, then write it back out to the same file but omitting the record to be deleted.

Another method that I have used on occasion is to just mark the record for deletion, such as change the first character of the first item in the record to something that is not likely to be used for that item. If the first item in the record is someone's name then change the first character to '~'. Then when your program reads the file just have it ignore records whose first character is '~'. This is the same concept that databases use to delete records.

thanks for the info
but can u give a coding for 2nd method that u just mentioned that fits to my program??

btw i want a specific record to be deleted..
e.g user wuld be asked to name the patient name..after taking input it wil delete tht record from the file!..
can u plz help??

If you are asking me to write the code for you, my answer is no. What you will have to do is search the file for the patient's name and when found mark it for deletion. You will want to call fstream's tellg() before reading each pathent's record so that you can easily go back to the beginning of the record (by calling seekg()) when you have to mark it for deletion.

This is where good file design makes your programming efforts a lot easier. Rather than putting each patient's data on separate lines, put then all on one line so that you can read the entire patheit's record in one shot using getlin(). You can use tab character to separate individual items in the record. Something like this:

Also note that the hospital record parameter should be passed by reference, not by value. That will speed up your program quite a bit.

Next thing about file format is that you don't have to write the titles of each field inthe file.

ostream &operator<<(ostream &stream, hospital& o)
{
	stream<<o.name<< '\t'
	   <<o.id<<'\t'
   	   <<o.telephone<<'\t'
	   <<o.numday<<'\t'
	   <<o.numday*2000<<'\t'
	   <<o.roomnum<<"\n";
	return stream;
}

dude im jst a starter at C++...
but i wil try this method..
btw can u tell how to search and compare the patient's name to input the user gives to the program?
is there any function for it??!!

Now for that operator>>. That is supposed to read a record from the file, not write to it. >> is for read and << is for write. So it doesn't make any sense to ask all those questions in the operator>> function.

Do you know how to read a file? Search and compare is the same thing except after every read you just compare the name.

hospital record;
char Name[] = "John Doe";
ifstream in("filename.txt"); // open the file
// use your operator>> to read each record
while( in >> record )
{
   if( strcmp(record.name,Name) == 0 )
   {
      // got it
   }
}

i tried it but culdnt get it successfully..:(
neway thankx alot for the help!

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.