File Handling hospital management c++

Hi can anybody help me what i did wrong. I have added doctor information and now i just want to see the details i have stored. please help me with the cod. i really appreciate the help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

void doctordetails(){

	int doctordetail;
		cout<<"\t***************  Doctor Details  *****************\n\n\n";
		cout<<"1. Add New Doctor\n\n2. View Doctor Details.\n\n";
		cin>>doctordetail;
		
			ofstream doctor;
			doctor.open("doctordetails.txt",ios::app|ios::binary);
			doctor.close();
		system ("cls");
		switch(doctordetail){
			case 1:
	char name[50],field[50];
	int num,age;
	ofstream doctor;
	doctor.open("doctordetails.txt",ios::app|ios::binary);
		cin.sync();
		cout<<"\nEnter The Doctor Name = ";
		cin.getline(name,50);
		cout<<"\nEnter the Doctors Field = ";
		cin.getline(field,50);
		cout<<"\nEnter Age = ";
		cin>>age;
		cout<<"\nEnter Experience(in years) = ";
		cin>>num;
			cout<<"\nName = "<<name;
			cout<<"\nAge = "<<age;
			cout<<"\nExperience = "<<num;
			cout<<"\nField = "<<field;
		cout<<"\n\nYour Entry Has been saved ";
		doctor.close();
		break;
		case 2:
		ifstream doctor;
		doctor.open("doctordetails.txt",ios::app|ios::binary);
			break;
			
		}
Last edited on
Hello HS05669,

I see several problems with your code.

First you need some blank lines and watch your indenting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
void doctordetails()
{
    int doctordetail;

    cout << "\t***************  Doctor Details  *****************\n\n\n";
    cout <<
        "\n1. Add New Doctor"
        "\n2. View Doctor Details."
        "Enter choice: ";
    cin >> doctordetail;

    ofstream doctor;

    doctor.open("doctordetails.txt", ios::app | ios::binary);

    doctor.close();

    system("cls");

    switch (doctordetail)
    {
        case 1:
            char name[50], field[50];
            int num, age;

            ofstream doctor;

            doctor.open("doctordetails.txt", ios::app | ios::binary);

            cin.sync();

            cout << "\nEnter The Doctor Name = ";
            cin.getline(name, 50);

            cout << "\nEnter the Doctors Field = ";
            cin.getline(field, 50);

            cout << "\nEnter Age = ";
            cin >> age;

            cout << "\nEnter Experience(in years) = ";
            cin >> num;

            cout << "\nName = " << name;
            cout << "\nAge = " << age;
            cout << "\nExperience = " << num;
            cout << "\nField = " << field;
            cout << "\n\nYour Entry Has been saved ";

            doctor.close();

            break;
        case 2:
            ifstream doctor;

            doctor.open("doctordetails.txt", ios::app | ios::binary);

            break;
    }
}

The blank lines make it easier to read and the first benefit is to you.

Line 6 is just another way to write the "cout" statement that is easier to visualize how what is displayed on the screen will look. Also easier to adjust or add to like I did with the last line.

Lines 12 and 14 define and open the file, but yo could also write it as
ofstream doctor("doctordetails.txt", ios::app | ios::binary);

Any how you open the file stream and then close it. Is there a reason you did this?

I also wondering why you need "binary" mode for the files?

The "switch" starts OK, although I would have used "choice" instead of "doctordetail". "choice" is more descriptive of what it is used for, but it is always your choice for variable names.

In "case 1" you start by defining variables. You can do this, but I have always found that if you do they need to be in a set of {}s.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
switch (doctordetail)
{
    case 1:
    {
        char name[50], field[50];
        int num, age;

        ofstream doctor("doctordetails.txt", ios::app | ios::binary);

        doctor.open("doctordetails.txt", ios::app | ios::binary);

        cin.sync();

        cout << "\nEnter The Doctor Name = ";
        cin.getline(name, 50);

        cout << "\nEnter the Doctors Field = ";
        cin.getline(field, 50);

        cout << "\nEnter Age = ";
        cin >> age;

        cout << "\nEnter Experience(in years) = ";
        cin >> num;

        cout << "\nName = " << name;
        cout << "\nAge = " << age;
        cout << "\nExperience = " << num;
        cout << "\nField = " << field;
        cout << "\n\nYour Entry Has been saved ";

        doctor.close();
    }
        break;

The only problem with this is that the variables are local to the block and lost when you reach the closing }. Best to define those variables outside the "switch".

The rest of the case looks OK except you take input from the keyboard then write what you just input to the screen, but never write to your output file before you close it.

There is no reason or advantage I can see for opening the file in the case statement. Just remove the close on line 16 and on line 50. The file stream will close when the function looses scope.

"case 2" is similar to "case 1". To define variables in the case statement you will need the {}s, but the file stream and any other variables should already be defined before the switch.

Lastly it is best to provide enough code that can be compiled and run for testing. There may be problems before you get to the function.

Aslo if there is an input file if is very helpful to share that with everyone, so that everyone can be using the same information. Also it may show problems that you did not mention or even know about yet. If the file should be large a good sample for testing will work. Do not count on someone fixing your code just to write to a file to be able to use it for input.

Andy
Hi Andy thank you so much for replying I really appreciate it. so basically i have a project where i have to create a management system of a hospital. it is not complete and i am still working on . so basically in the switch above there are 2 options 1 for adding new doctors to file and 2 for showing all the doctors that are currently there. what i basically want is that all the information about the doctors that i have entered in should be shown when i press 2.

I made some corrections to the best of my knowledge but if you could help me out once more i will really appreciate it. I have also put out my entire code.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>    
#include<iostream> 
#include<fstream> 
#include<string> 
using namespace std; 
	

void doctordetails(){
	char name[50],field[50];
	int num,age;
	fstream doctor;
	int doctordetail;
		cout<<"\t***************  Doctor Details  *****************\n\n\n";
		cout<<"1. Add New Doctor\n\n2. View All Doctors.\n\n";
		cin>>doctordetail;
		system ("cls");
		switch(doctordetail){
		case 1:
		ofstream doctor;
		doctor.open("doctordetails.txt");
		cin.sync();
		cout<<"\nEnter The Doctor Name = ";
		cin.getline(name,50);
		cout<<"\nEnter the Doctors Field = ";
		cin.getline(field,50);
		cout<<"\nEnter Age = ";
		cin>>age;
		cout<<"\nEnter Experience(in years) = ";
		cin>>num;
		system("cls");
		cout<<"\n\nYour Entry Has been saved ";

		break;

				
		}
}
void patientdetails(){
		cout<<"\t***************  Patient Details  *****************\n\n\n";
		cout<<"1. Add New Patient\n\n2. View Patient Details.";
}
void hospitalservices(){
		cout<<"\t***************  Hospital Services  *****************\n\n\n";
		cout<<"1. Add New Hospital Services\n\n2. View Hospital Services.";
}
int main(){
	int choice1, choice2;
	cout<<"\t***************  Hospital Managment System  *****************\n\n\n";
	cout<<"\n\nPlease Choose from the Menu\n\n1. Admin\n\n2. Doctor\n\n3. Patient\n\n";
	cin>>choice1;
	system("cls");
	if(choice1==1){	
		cout<<"\t***************  Admin  *****************\n\n\n";
		cout<<"1. Doctor Details\n\n2. Patient Details\n\n3. Hospital Services\n\n";
		cin>>choice2;
		system("cls");
		switch(choice2)
		{
		case 1: 
		doctordetails();
		break;
		case 2: 
		patientdetails();
		break;
		case 3: 
		hospitalservices();
		break;
		default:
			cout<<"Error!! Please Choose from the menu";
		break;
		}

		}		
 return 0;
}
Last edited on
Hello HS05669,

Sorry for the delay. My computer did a restart a couple of times and I lost track of what I was working on.

Also your program code is so hard to read I had to rework it twice before I could figure out what was wrong.

To start with "stdio.h" is a C header file and has no place in a C++ program. If you insist on including it use "cstdio".

In the function "void doctordetails()" you define 2 character arrays. Just us a "std::string" here. The arrays are not needed.

"num" has its uses as a variable name, but you should call it 'yrsExp" or something like that. It makes more sense and is easier to understand.

Next you define "fstream doctor". Why? You never use it. It could also be a conflict when you define "ofstream doctor" later.

I did this with the menu in the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
do
{
    CLS;

    cout << "\n***************  Doctor Details  *****************\n\n";
    cout <<
        "1. Add New Doctor.\n"
        "2. View All Doctors.\n"
        "3. Exit.\n"
        " Enter Choice: ";
    cin >> choice;

    if (!std::cin || (choice < 1 || choice > 3))
    {
        if (!std::cin)
        {
            std::cout << "\n     Invalid entry! Must be a number.\n";

            std::cin.clear();
        }
        else if (choice < 1 || choice > 3)
        {
            std::cout << "\n     Invalid choice! Try again.\n";
        }

        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
        std::cout << "\n Press Enter to continue: ";
        std::cin.get();
    }
} while (choice < 1 || choice > 3);

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. 

Line 6 can be done in 1 line or with several "cout" statements, but this gives you a better idea of what you are working with and it is still 1 line. It just spans several line now.

The following "cin" will flush the output buffer and print everything to the screen before the "cin" takes any input.

The first if statement takes care of any problem and stays in the do/while loop until a valid choice is made.

You could take lines 13 - 29 and put them in a function with at least 2 parameters for "min" and "max". And if you make it generic enough you could add 2 parameters for the messages.

The switch is a good start, but only allows 1 entry before you end up back at the main menu, the first menu.

Either the case statement or the whole switch can be put in a do/while loop to keep it going for more than 1 entry.

Also most of the function can be put in a do/while loop until you make the choice to "exit" and back up to the previous menu. This idea is needed in several p;aced so you do not end up at the main menu to soon.

That is the only function I have worked on so far and you will also need to write something to the file before the case statement ends or it is all for nothing.

When writing to the file yo will also need to be thinking about how you will need to read the file. "name" and "field" may have spaces in them that you will need to keep.

Andy
Topic archived. No new replies allowed.