Click here to Skip to main content
14,385,111 members
Rate this:
Please Sign up or sign in to vote.
See more:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
class hospappt
{
	char uname[20];
	int idno;
	int age;
	int pno;
	int id;
	int dp;
	int hospid;
	char hospname[50];
	char email[30];
	char dpt[30];
	char gender;
	char docname[50];
	public:
	void admin();
	void user();
	void insert();
	void Delete();
	void display();
	void dept();
};
void hospappt::admin()
{
	clrscr();
	int c;
	char ch;
	do
	{
		cout<<"\n\t\t****ADMIN MENU****";
		cout<<"\n\t\t1. INSERT INFORMATION";
		cout<<"\n\t\t2. DISPLAY INFORMATION";
		cout<<"\n\t\t3. DELETE INFORMATION";
		cout<<"\n\t\t4.RETURN TO MAIN";
		cout<<"\n\t\tPLEASE ENTER YOUR CHOICE: ";
		cin>>c;
		switch(c)
		{
		case 1:
			insert();
			break;
		case 2:
			display();
			break;
		case 3:
			Delete();
			break;
		case 4:
			return;
		default:
			cout<<"\n\t\tWRONG OPTION!!!!!";
			return;
		}
		cout<<"\n\tDO YOU WANT TO CONTINUE WITH ADMIN MODE?(Y\N)";
		cin>>ch;
	}while(ch=='Y'||ch=='y');
}

void hospappt::insert()
{
	clrscr();
	int n,i;
	hospappt h;
	ofstream d;
	d.open("doct.dat",ios::app|ios::binary);
	cout<<"\nENTER NUMBER OF DOCTORS YOU WOULD LIKE TO ADD:";
	cin>>n;
	for(i=0;i<n;++i)
	{
		cout<<"\n\t\tPLEASE ENTER THE FOLLOWING DETAILS";
		cout<<"\n\t\tHOSPITAL ID: ";
		cin>>hospid;
		cout<<"\n\t\tHOSPITAL NAME: ";
		gets(hospname);
		cout<<"\n\t\tDEPARTMENT: ";
		gets(dpt);
		cout<<"\n\t\tNAME OF DOCTOR:  ";
		gets(docname);
		d.write((char*)&h,sizeof(h));
		cout<<"\n\t\t!!!!!RECORD SUCCESSFULLY INSERTED!!!!!";
	}
	d.close();
	return;
}

void hospappt::display()
{
	hospappt h;
	int id;
	char dep[30];
	ifstream fin;
	fin.open("doct.dat",ios::app|ios::binary);
	cout<<"ENTER ID NUMBER:";
	cin>>id;
	cout<<"ENTER DEPARTMENT:";
	gets(dep);
	while(fin.read((char*)&h,sizeof(h)))
	{
		if(id==hospid)
		{
			if(strcmpi(dpt,dep))
			{
				cout<<"HOSPITAL NAME: "<<hospname;
				cout<<"DEPARTMENT   : "<<dpt;
				cout<<"DOCTOR       : "<<docname;
			}
		}
		else
		{
			cout<<"HOSPITAL NOT FOUND!!!";
			return;
		}
	}
	fin.close();
	return;
}


void hospappt::dept()
{
	clrscr();
	cout<<"\n\t\t*****DEPARTMENT*******";
	cout<<"\n\t1. GENERAL MEDICINE";
	cout<<"\n\t2. CARDIOLOGY";
	cout<<"\n\t3. ENT";
	cout<<"\n\t4. GASTROENTEROLOGY";
	cout<<"\n\t5. GYNAECOLOGY";
	cout<<"\n\t6. PEDIATRICIAN";
	cout<<"\n\t7. NEUROLOGY";
	cout<<"\n\t8. ONCOLOGY";
	cout<<"\n\t9. OPTHALMOLOGY";
	cout<<"\n\t10.ORTHOPAEDICS";
	cout<<"\n\t     PLEASE ENTER YOUR OPTION:";
	cin>>dp;
	return;
}
void hospappt::Delete()
{
	return;
}
void main()
{
	hospappt h;
	h.admin();
	cout<<"SMILE";
	getch();
	return;
}


What I have tried:

In the above program the display function does not work..even thogh I insert information, when I try to display it does not give the desired output (i.e. The informaiton i inserted through insert function)
I hope to get an answer/correction very soon.

(p.s. the program is not completed as I cannot proceed without getting this error rectified) =)
Posted
Updated 8-Nov-19 4:24am
v4
Rate this:
Please Sign up or sign in to vote.

Solution 1

void hospappt::insert()
{
 clrscr();
 int n,i;
 hospappt h; // this is a local object and is redundant
 ofstream d;
 d.open("doct.dat",ios::app|ios::binary);
 cout>n;
 for(i=0;i>hospid;
  cout<<"\n\t\tHOSPITAL NAME: ";
  gets(hospname);
  cout<<"\n\t\tDEPARTMENT: ";
  gets(dpt);
  cout<<"\n\t\tNAME OF DOCTOR:  ";
  gets(docname);
  d.write((char*)&h,sizeof(h)); // you are writing an empty object
  cout<<"\n\t\t!!!!!RECORD SUCCESSFULLY INSERTED!!!!!";
 }
 d.close();
 return;
}


You should not create a local copy of your class inside one of its methods. Use the this pointer to refer to the variables inside the object.

And please switch off your caps lock; typing in all upper case is considered shouting on the internet. Also, please do not claim your project is urgent, it is not to anyone here.
   
Comments
AISHA SH 7-Nov-19 14:18pm
   
Thankyou!
But is there any other option other than *this* pointer?
Richard MacCutchan 7-Nov-19 14:27pm
   
The issue is that inside a method of the class, all the variables are directly accessible. You can do this explicitly with this:
 cin >> this->variable;

or implicitly just by using the variable's name, as you have in your code. The key being that the method is effectively inside the class.

When you come to write out the record to permanent storage you need to use a serializer which writes each variable in turn. Alternatively in your simple project you could write and read the object inside your main method.
AISHA SH 7-Nov-19 14:44pm
   
Ok
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
C++ project...display not working(runtime error)

When asking for help, it is generally considered a good idea to tell what is the error and its position.
Quote:
when I try to display it does not give the desired output..

And what is the desired output ?

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
class hospappt
{
	char uname[20];
	int idno;
	int age;
	int pno;
	int id;
	int dp;
	int hospid;
	char hospname[50];
	char email[30];
	char dpt[30];
	char gender;
	char docname[50];
	public:
	void admin();
	void user();
	void insert();
	void Delete();
	void display();
	void dept();
};
void hospappt::admin()
{
	clrscr();
	int c;
	char ch;
	do
	{
		cout<<"\n\t\t****ADMIN MENU****";
		cout<<"\n\t\t1. INSERT INFORMATION";
		cout<<"\n\t\t2. DISPLAY INFORMATION";
		cout<<"\n\t\t3. DELETE INFORMATION";
		cout<<"\n\t\t4.RETURN TO MAIN";
		cout<<"\n\t\tPLEASE ENTER YOUR CHOICE: ";
		cin>>c;
		switch(c)
		{
		case 1:
			insert();
			break;
		case 2:
			display();
			break;
		case 3:
			Delete();
			break;
		case 4:
			return;
		default:
			cout<<"\n\t\tWRONG OPTION!!!!!";
			return;
		}
		cout<<"\n\tDO YOU WANT TO CONTINUE WITH ADMIN MODE?(Y\N)";
		cin>>ch;
	}while(ch=='Y'||ch=='y');
}

void hospappt::insert()
{
	clrscr();
	int n,i;
	hospappt h;
	ofstream d;
	d.open("doct.dat",ios::app|ios::binary);
	cout<<"\nENTER NUMBER OF DOCTORS YOU WOULD LIKE TO ADD:";
	cin>>n;
	for(i=0;i<n;++i)
	{
		cout<<"\n\t\tPLEASE ENTER THE FOLLOWING DETAILS";
		cout<<"\n\t\tHOSPITAL ID: ";
		cin>>hospid;
		cout<<"\n\t\tHOSPITAL NAME: ";
		gets(hospname);
		cout<<"\n\t\tDEPARTMENT: ";
		gets(dpt);
		cout<<"\n\t\tNAME OF DOCTOR:  ";
		gets(docname);
		d.write((char*)&h,sizeof(h));
		cout<<"\n\t\t!!!!!RECORD SUCCESSFULLY INSERTED!!!!!";
	}
	d.close();
	return;
}

void hospappt::display()
{
	hospappt h;
	int id;
	char dep[30];
	ifstream fin;
	fin.open("doct.dat",ios::app|ios::binary);
	cout<<"ENTER ID NUMBER:";
	cin>>id;
	cout<<"ENTER DEPARTMENT:";
	gets(dep);
	while(fin.read((char*)&h,sizeof(h)))
	{
		if(id==hospid)
		{
			if(strcmpi(dpt,dep))
			{
				cout<<"HOSPITAL NAME: "<<hospname;
				cout<<"DEPARTMENT   : "<<dpt;
				cout<<"DOCTOR       : "<<docname;
			}
		}
		else
		{
			cout<<"HOSPITAL NOT FOUND!!!";
			return;
		}
	}
	fin.close();
	return;
}


void hospappt::dept()
{
	clrscr();
	cout<<"\n\t\t*****DEPARTMENT*******";
	cout<<"\n\t1. GENERAL MEDICINE";
	cout<<"\n\t2. CARDIOLOGY";
	cout<<"\n\t3. ENT";
	cout<<"\n\t4. GASTROENTEROLOGY";
	cout<<"\n\t5. GYNAECOLOGY";
	cout<<"\n\t6. PEDIATRICIAN";
	cout<<"\n\t7. NEUROLOGY";
	cout<<"\n\t8. ONCOLOGY";
	cout<<"\n\t9. OPTHALMOLOGY";
	cout<<"\n\t10.ORTHOPAEDICS";
	cout<<"\n\t     PLEASE ENTER YOUR OPTION:";
	cin>>dp;
	return;
}
void hospappt::Delete()
{
	return;
}
void main()
{
	hospappt h;
	h.admin();
	cout<<"SMILE";
	getch();
	return;
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
   
Comments
Stefan_Lang 11-Nov-19 6:16am
   
'It also helps spotting structures mistakes.'
Talking about structure mistakes, the public keyword should _not_ be indented. I failed to spot it initially and wondered how that code could even compile...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100