how to fill other indexes of array after breaking it ...

Pages: 12
i am working on my college project it is about hospital management system. all i want that the user enter patient record and the loop of array break after filling one index and it should continue after other processes in the program ...if the user want to enter another patient record it should not fiill the first index but continue from the next one..
the details are stored in a structure array of size 10 as data[10];
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

do{
  	for(int i=0; i<10; i++) {
				system("cls");
				if(i=1);
				i++;
				cout<<"\t\t\t\t\t\t\t--------------------------------------------"<<endl;
				cout<<"\t\t\t\t\t\t\t|         Enter Patient Details            |"<<endl;
				cout<<"\t\t\t\t\t\t\t--------------------------------------------"<<endl;
				cout<<"\n\t\t\t\t\t\t\t      ID :: ";
				cin.ignore();
				cin>>data[i].id;
				cout<<"\n\t\t\t\t\t\t\t    Name :: ";
				cin.ignore();
				gets(data[i].name);
				cout<<"\n\t\t\t\t\t\t\t Address ::";
				
				gets(data[i].adress);
				cout<<"\n\t\t\t\t\t\t\t Contact ::";
				
				cin>>data[i].contact;
				cout<<"\n\t\t\t\t\t\t\tCNIC/NTN :: ";
				cin.ignore();
				cin>>data[i].cnic;
				cout<<"\n\t\t\t\t\t\t\t     Age :: ";
				cin>>data[i].age;
				cout<<"\n\t\t\t\t\t\t\t     Sex :: ";
				cin.ignore();
				cin>>data[i].sex;
				cout<<"\n\t\t\t\t\t\t\tBlood Group:: ";
				cin>>data[i].blood_gp;
				cout<<"\n\t\t\t\t\t\t\tPast Disease(if any) :: ";
				cin>>data[i].disease_past;
				cout<<"\n\t\t\t\t\t\t\t\aPatient added successfully!.\n";
				cout<<"\n\t\t\t\t\t\t\tPress Y to Add aonother patient/Press N to go to main menu\n";
				break;
			}
		} while(getch()=='y' || getch()=='Y');
		{
			system("cls");
			goto a;
		}

		goto b;
	}
Last edited on
You need to wrap whatever you're repeating inside the while loop that loops on whether or not the user enters 'y' at the end, and increment the index each time.

Don't "break;" at the end of the body, it defeats the purpose of the loop.
And don't use goto unless there's no better option. You are already using 2 gotos and your code isn't working.

You should be able to use this as a template for what you're trying to do:

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
// Example program
#include <iostream>
#include <string>
using namespace std;

struct PatientRecord {
    string name;
    string id;
};

int main()
{
    const int MaxPatients = 100;
    PatientRecord records[MaxPatients];
    
    int pat_index = 0;
    
    char again = 'y';
    do
    {
        cout << "-- Enter patient details --\n";
        
        cout << "Patient ID: ";
        cin >> records[pat_index].id;
        
        cout << "Patient Name: ";
        cin >> records[pat_index].name; // use getline if you need to get spaces
        
        pat_index++;
        
        cout << "Enter another patient? (y/n)";
        cin >> again;

    } while (again == 'y' && pat_index < MaxPatients);
}
Last edited on
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
do{
  	for(int i=0; i<10; i++) {
				system("cls");
				if(i=1); //this is not doing anything, like saying if(true)
				i++;
				cout<<"\t\t\t\t\t\t\t--------------------------------------------"<<endl;
				cout<<"\t\t\t\t\t\t\t|         Enter Patient Details            |"<<endl;
				cout<<"\t\t\t\t\t\t\t--------------------------------------------"<<endl;
				cout<<"\n\t\t\t\t\t\t\t      ID :: ";
				cin.ignore();
				cin>>data[i].id;
				cout<<"\n\t\t\t\t\t\t\t    Name :: ";
				cin.ignore();
				gets(data[i].name);
				cout<<"\n\t\t\t\t\t\t\t Address ::";
				
				gets(data[i].adress);
				cout<<"\n\t\t\t\t\t\t\t Contact ::";
				
				cin>>data[i].contact;
				cout<<"\n\t\t\t\t\t\t\tCNIC/NTN :: ";
				cin.ignore();
				cin>>data[i].cnic;
				cout<<"\n\t\t\t\t\t\t\t     Age :: ";
				cin>>data[i].age;
				cout<<"\n\t\t\t\t\t\t\t     Sex :: ";
				cin.ignore();
				cin>>data[i].sex;
				cout<<"\n\t\t\t\t\t\t\tBlood Group:: ";
				cin>>data[i].blood_gp;
				cout<<"\n\t\t\t\t\t\t\tPast Disease(if any) :: ";
				cin>>data[i].disease_past;
				cout<<"\n\t\t\t\t\t\t\t\aPatient added successfully!.\n";
				cout<<"\n\t\t\t\t\t\t\tPress Y to Add aonother patient/Press N to go to main menu\n";
				break;
			}
		} while(getch()=='y' || getch()=='Y');
		{
			system("cls");
			goto a;
		}

		goto b;
	}


Can you post more of your code? Enough for us to be able to compile and test it.
Last edited on
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<iostream>
#include<string>
#include<conio.h>
#include<windows.h>
#include<time.h>
#pragma comment(lib, "user32")
using namespace std;
struct patient_info {
	int id;
	char name[22];
	char adress[200];
	char contact[12];
	char cnic[13];
	int age;
	char sex[8];
	char blood_gp[5];
	char disease_past[50];
};
////////////////////////////Function Declaration//////////////////////////////
int login(void);
int header(void);
int footer(void);
int lpd(void);
int load(void);
int menu(void);
void gotoxy(short x, short y);
////////////////////////////////////Main Function/////////////////////////////////////////
int main() {

	login();
}
int login(void) { //login
	::SendMessage(::GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x20000000);
	system("color 37");
//	textbackground(3);
	string username="";
	string pass="";
	char ch;
	header();
	cout<<"\n\n\n\n\t\t\t\t\t\t\t\t  HOSPITAL MANAGEMENT SYSTEM \n\n";
	cout<<"\t\t\t\t\t\t\t\t------------------------------";
	cout<<"\n\t\t\t\t\t\t\t\t\t     LOGIN \n";
	cout<<"\t\t\t\t\t\t\t\t------------------------------\n\n";
	cout<<"\t\t\t\t\t\t\t\t\fEnter Username:";
	cin>>username;
	cout << "\t\t\t\t\t\t\t\t\fEnter Password:";
	ch = _getch();
	while(ch != 13) { //character 13 is enter
		pass.push_back(ch);
		cout << '*';
		ch = _getch();
	}
	if(username=="atta"&&pass == "5521") {
		cout << "\n\t\t\t\t\t\t\t\tAccess Granted!\t\fLOADING \n\t";

		cout<<"   \t\t\t\t\t\t";
		load();
		menu();
	} else {
		cout << "\n\n\t\t\t\t\t\t\t\t\aAccess Aborted...\n\t\t\t\t\t\t\t\tPlease Try Again\n\n";
		system("pause");
		::SendMessage(::GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x20000000);
		system("CLS");
		login();
	}
}
int lpd(void) {
	system("cls");
	cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
	cout<<"\t\t\t\t\t@@ _______________________________________________________________________________________ @@\n";
	cout<<"\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout<<"\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout<<"\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout<<"\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout<<"\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout<<"\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout<<"\t\t\t\t\t@@|                                  WELCOME TO                                           |@@\n";
	cout<<"\t\t\t\t\t@@|                                                                                       |@@\n";
	cout<<"\t\t\t\t\t@@|                           HOSPITAL MANAGEMENT SYSTEM                                  |@@\n";
	cout<<"\t\t\t\t\t@@|                                                                                       |@@\n";
	cout<<"\t\t\t\t\t@@|                                                                                       |@@\n";
	cout<<"\t\t\t\t\t@@|                                                                                       |@@\n";
	cout<<"\t\t\t\t\t@@|                                                                                       |@@\n";
	cout<<"\t\t\t\t\t@@|                                                                                       |@@\n";
	cout<<"\t\t\t\t\t@@|                                                 -Brought To You by Atta ul Munim      |@@\n";
	cout<<"\t\t\t\t\t@@|_______________________________________________________________________________________|@@\n";
	cout<<"\t\t\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n\n\n\t\t\t\t\t";

}
int header(void) {
	time_t rawtime;
	struct tm * timeinfo;
	patient_info p1;
	time (&rawtime);
	timeinfo = localtime ( &rawtime );
	cout<<"\n\n\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tTIME::"<< asctime (timeinfo);
}
int footer(void) {
	cout<<"\n\n\t\t\t\t Developed By::\n \t\t\t\t\t\t Atta UL Munim"<<endl;
}
int load(void) {
	for(int i=0; i<12; i++) {
		cout<<"\xdd\xdd\xdd";
		Sleep(100);
	}
	system("cls");
}
void gotoxy(short x, short y) {         //definition of gotoxy function//
	COORD pos = {x,y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

int menu(void) {
	lpd();
	system("pause");
	system("cls");
	patient_info data[10];
	int num;
	int n;
	int b=0;
	int option;
	int i;
	char sr[13];
	cout<<"\n\n\n\t\t\t\t\t\t\t\t  HOSPITAL MANAGEMENT SYSTEM \n";
b:
	cout<<"\n\n\t\t\t\t\t\tPlease,  Choose from the following Options: \n\n";
	cout<<"\t\t\t\t\t\t _________________________________________________________________ \n";
	cout<<"\t\t\t\t\t\t|                                           	                  |\n";
	cout<<"\t\t\t\t\t\t|             1  >> Add New Patient Record                        |\n";
	cout<<"\t\t\t\t\t\t|             2  >> Search For A Patint                           |\n";
	cout<<"\t\t\t\t\t\t|             3  >> Delete Patient Record                         |\n";
	cout<<"\t\t\t\t\t\t|             4  >> Display All Admitted Patients                 |\n";
	cout<<"\t\t\t\t\t\t|             5  >> Exit the Program                              |\n";
	cout<<"\t\t\t\t\t\t|_________________________________________________________________|\n\n";
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
a:
	cout<<"\t\t\t\t\t\tEnter your choice--> ";
	cin>>i;
	system("cls");
	if(i>5||i<1) {
		cout<<"\n\n\n\t\t\t\t\t\t\t\t\aInvalid choice!";
		goto b;
	}
	if(i==1) {
		do {
			for(int i=0; i<10; i++) {
				system("cls");
				if(i=1);
				i++;
				cout<<"\t\t\t\t\t\t\t--------------------------------------------"<<endl;
				cout<<"\t\t\t\t\t\t\t|         Enter Patient Details            |"<<endl;
				cout<<"\t\t\t\t\t\t\t--------------------------------------------"<<endl;
				cout<<"\n\t\t\t\t\t\t\t      ID :: ";
				cin.ignore();
				cin>>data[i].id;
				cout<<"\n\t\t\t\t\t\t\t    Name :: ";
				cin.ignore();
				gets(data[i].name);
				cout<<"\n\t\t\t\t\t\t\t Address ::";
				
				gets(data[i].adress);
				cout<<"\n\t\t\t\t\t\t\t Contact ::";
				
				cin>>data[i].contact;
				cout<<"\n\t\t\t\t\t\t\tCNIC/NTN :: ";
				cin.ignore();
				cin>>data[i].cnic;
				cout<<"\n\t\t\t\t\t\t\t     Age :: ";
				cin>>data[i].age;
				cout<<"\n\t\t\t\t\t\t\t     Sex :: ";
				cin.ignore();
				cin>>data[i].sex;
				cout<<"\n\t\t\t\t\t\t\tBlood Group:: ";
				cin>>data[i].blood_gp;
				cout<<"\n\t\t\t\t\t\t\tPast Disease(if any) :: ";
				cin>>data[i].disease_past;
				cout<<"\n\t\t\t\t\t\t\t\aPatient added successfully!.\n";
				cout<<"\n\t\t\t\t\t\t\tPress Y to Add aonother patient/Press N to go to main menu\n";
				break;
			}
		} while(getch()=='y' || getch()=='Y');
		{
			system("cls");
			goto a;
		}

		goto b;
	}
	

	if(i==2) {
c:
		cout<<"\t\t\t\t\t\t\t------------------------"<<endl;
		cout<<"\t\t\t\t\t\t\t|  Search For A Patint  |"<<endl;
		cout<<"\t\t\t\t\t\t\t|-----------------------|"<<endl;
		cout<<"\t\t\t\t\t\t\t|1 >> Search by ID      |"<<endl;
		cout<<"\t\t\t\t\t\t\t|2 >> Search by CNIC/NTN|"<<endl;
		cout<<"\t\t\t\t\t\t\t------------------------"<<endl;
		cout<<"\t\t\t\t\t\t\tEnter Your Choice-->";
		cin>>option;

		switch(option) {
			case 1: {
				int k;
				cout<<"\t\t\t\tEnter Patieint id::";
				cin>>num;
				for(int i=0; i<10; i++)
					if(data[i].id==num)
						k=i;

				if(k==-1) {
					cout<<"\n\t\t\t\tInvalid ID!";
				} else {
					cout<<"\n\t\t\t\tPatient info\n";
					cout<<"\n\t\t\t\t**************************\n";
					cout<<"\n\t\t\t\t        ID ::"<<data[k].id<<endl;
					cout<<"\n\t\t\t\t      Name ::"<<data[k].name<<endl;
					cout<<"\n\t\t\t\t   Address ::"<<data[k].adress<<endl;
					cout<<"\n\t\t\t\t   Contact ::"<<data[k].contact<<endl;
					cout<<"\n\t\t\t\t  CNIC/NTN ::"<<data[k].cnic<<endl;
					cout<<"\n\t\t\t\t       Age ::"<<data[k].age<<endl;
					cout<<"\n\t\t\t\t       Sex ::"<<data[k].sex<<endl;
					cout<<"\n\t\t\t\tBlood Group ::"<<data[k].blood_gp<<endl;
					cout<<"\n\t\t\t\tPast Disease::"<<data[k].disease_past<<endl;
					cout<<"\n\t\t\t\t**************************\n";
					system("pause");
					system("cls");
					goto b;
				}
			}
			break;
			case 2: {
				int k;
				cout<<"\t\t\t\t\tSearch By Patient CNIC/NTN\n";
				cout<<"\t\t\t\t\tEnter Patient CNIC/NTN-->";
				cin>>sr;
				for(int i=0; i<10; i++)
					if(data[i].cnic==sr)
						k=i;

				if(k==-1) {
					cout<<"\n\t\t\t\t\tInvalid CNIC/NTN";
				}	else
					cout<<"\t\t\t\tPatient info";
				cout<<endl;
				cout<<"\n        \t\t\t\t ID :: "<<data[k].id<<endl;
				cout<<" \n     \t\t\t\t Name :: "<<data[k].name<<endl;
				cout<<"  \n  \t\t\t\tAddress :: "<<data[k].adress<<endl;
				cout<<"\n    \t\t\t\tContact :: "<<data[k].contact<<endl;
				cout<<"\n   \t\t\t\tCNIC/NTN :: "<<data[k].cnic<<endl;
				cout<<"\n        \t\t\t\tAge :: "<<data[k].age<<endl;
				cout<<"\n        \t\t\t\tSex :: "<<data[k].sex<<endl;
				cout<<" \n\t\t\t\tBlood Group:: "<<data[k].blood_gp<<endl;
				cout<<"\n\t\t\t\tPast Disease:: "<<data[k].disease_past<<endl;
				system("pause");
				system("cls");
				goto b;

			}
			break;
			default:
				cout<<"Invalid Choice!\a";
				goto c;
		}
	}
	if(i==3) {
		system("cls");
		cout<<"DELETE PATIENT RECORD\n";
		goto a;
	}
if(i==4){
	cout<<"DISPLAY ALL ADMITTED PATIENTS\n";
	goto b;
}
	if(i==5);
	{
		cout<<"Press R to run the program again.\t Press any key to exit. ";
		if(getch()=='r' || getch()=='R') {
			system("cls");
			login();
		} else {
			footer();
		}

		return 0;

	}
}
this is my code....i hope it makes all clear
i also need something that will remove the patient record from array.
You're spending WAY WAY too much time on eye candy.
The first 4 functions should just be deleted until you've got the bulk of the code working.

Get the actual functionality working first, then set about making it pleasing to the eye.
All you're doing at the moment is spending half your time on things that just keep getting in the way.

1. Remove all those goto's - they're not necessary at all.

> gets(data[i].name);
2. NEVER EVER USE gets().
https://en.cppreference.com/w/cpp/io/c/gets

Notes
The function provides no means to prevent buffer overflow of the destination array, given sufficiently long input string. std::gets was deprecated in C++11 and removed from C++14.

Besides, you shouldn't even be mixing the C and C++ I/O mechanisms.

3.
1
2
3
		for(int i=0; i<10; i++) {
				if(i=1);
				i++;

Quick quiz, what is the value of I each time around this loop?

4. Modularisation
Your menu function needs to be broken up.
1
2
3
if(i==1) doAddPatient();
else if(i==2) doSearchPatient();
/// 

Each menu option should be a direct call to a function to do that work.

5. Lay off the endless recursion of login calling login calling menu calling login....
Use proper call/return.




I have been trying to clean up your code. I have run into many issues. Namely when you have an int function then you must return an int value or change the functions to void functions.
Make each function do one thing, and do it well.
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<iostream>
#include<string>
#include<cstring>
//#include<conio.h>
//#include<windows.h>
//#include<time.h>
//#pragma comment(lib, "user32")
using namespace std;
struct patient_info {
  int id;
  char name[22];
  char adress[200];
  char contact[12];
  char cnic[13];
  int age;
  char sex[8];
  char blood_gp[5];
  char disease_past[50];
};
////////////////////////////Function Declaration//////////////////////////////

// Deleted distraction fluff
//int login(void);
//int header(void);
//int footer(void);
//int lpd(void);
//int load(void);

// This gets a lot easier if you start using actual C++ and
// have vector<patient_info> data;
int menu(patient_info data[], int &num_patients);
//void gotoxy(short x, short y);


int main()
{
  patient_info data[10];
  int num_patients = 0;
  do {
    ;
  } while ( menu(data, num_patients) != 5 );
}

void doAddPatient(patient_info data[], int &num_patients)
{
  cout << "Enter Patient Details" << endl;
  cout << "ID :: ";
  cin.ignore();
  cin >> data[num_patients].id;
  cout << "Name :: ";
  cin.ignore();
  cin.getline(data[num_patients].name,sizeof(data[num_patients].name));
  cout << "Address ::";
  cin.getline(data[num_patients].adress,sizeof(data[num_patients].adress));
  cout << "Contact ::";
  cin >> data[num_patients].contact;
  cout << "CNIC/NTN :: ";
  cin.ignore();
  cin >> data[num_patients].cnic;
  cout << "Age :: ";
  cin >> data[num_patients].age;
  cout << "Sex :: ";
  cin.ignore();
  cin >> data[num_patients].sex;
  cout << "Blood Group:: ";
  cin >> data[num_patients].blood_gp;
  cout << "Past Disease(if any) :: ";
  cin >> data[num_patients].disease_past;
  num_patients++;
}

void printPatient(const patient_info &patient)
{
  cout << "Patient info\n";
  cout << "        ID ::" << patient.id << endl;
  cout << "      Name ::" << patient.name << endl;
  cout << "   Address ::" << patient.adress << endl;
  cout << "   Contact ::" << patient.contact << endl;
  cout << "  CNIC/NTN ::" << patient.cnic << endl;
  cout << "       Age ::" << patient.age << endl;
  cout << "       Sex ::" << patient.sex << endl;
  cout << "Blood Group ::" << patient.blood_gp << endl;
  cout << "Past Disease::" << patient.disease_past << endl;
}

void doSearchID(patient_info data[], int &num_patients)
{
  cout << "Enter Patieint id::";
  int num;
  cin >> num;
  for (int i = 0; i < num_patients; i++) {
    if (data[i].id == num) {
      printPatient(data[i]);
    }
  }
}

void doSearchCNIC_NTN(patient_info data[], int &num_patients)
{
  char sr[13];
  cout << "Search By Patient CNIC/NTN\n";
  cout << "Enter Patient CNIC/NTN-->";
  cin >> sr;
  for (int i = 0; i < num_patients; i++) {
    if ( strcmp(data[i].cnic,sr) == 0 ) {
      printPatient(data[i]);
    }
  }
}

void doSearch(patient_info data[], int &num_patients)
{
  int option;
  cout << "Search For A Patint" << endl;
  cout << "1 >> Search by ID" << endl;
  cout << "2 >> Search by CNIC/NTN" << endl;
  cout << "Enter Your Choice-->";
  cin >> option;
  if ( option == 1 )
    doSearchID(data, num_patients);
  else if ( option == 2 )
    doSearchCNIC_NTN(data, num_patients);
}

int menu(patient_info data[], int &num_patients)
{
  int option;
  cout << "Please,  Choose from the following Options: \n\n";
  cout << "1  >> Add New Patient Record\n";
  cout << "2  >> Search For A Patint\n";
  cout << "3  >> Delete Patient Record\n";
  cout << "4  >> Display All Admitted Patients\n";
  cout << "5  >> Exit the Program\n";

  do {
    cout << "Enter your choice--> ";
    cin >> option;
  } while ( option < 1 || option > 5 );

  if ( option == 1 ) doAddPatient(data, num_patients);
  else if ( option == 2 ) doSearch(data, num_patients);
  // add two more lines here to call two more functions
  return option;
}
i have tried the above one but this is not in our course , the professor won't accept it, i think there should be an alternative way for this to be done,also i am using dev c++ compiler, why clrscr();
is not working in it.
by the way thanks for help.
> i have tried the above one but this is not in our course , the professor won't accept it
What exactly in the corrections I posted isn't on your course?

If what you posted is indicative of what you're being taught, you'd be better off dropping the course now and finding a better tutor. Anyone telling new students that 'gets' even exists should be ashamed of themselves.

What you're being taught is windows centric archaic "c++ lite".

> also i am using dev c++ compiler, why clrscr(); is not working in it.
Because it's a function from a bygone era, and there's no obligation for any particular compiler to make sense of it.

You as a student should be learning standard C++ without ANY dependencies on any particular OS or compiler.
is it correct. i am trying to delete the record of patient but it is getting alot of errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void dodelete(patient_info data[], int &num_patients ){
	int x;
	cout<<"Enter Patient id ";
	cin>>x;
	int n=sizeof(data)/sizeof(data[0]);
for(int i=0; i<num_patients; i++)
if(data[i]==x)
break;
if (i<n)
{
	n=n-1;
	for(int j=i; j<num_patients;j++)
	data[j]=data[j+1];
	cout<<"patient record deleted";
}
else
cout<<"Invalid choice";
}
When an array is passed to a function it "decays" into a pointer. At that point the idiom sizeof(data)/sizeof(data[0]) doesn't work (since sizeof data will just be the size of a pointer, not the size in bytes of the array).

Another problem with this idiom is that even if it worked it would give you the total size of the array (the "capacity"), not the current "in-use" size.

However, you are passing num_patients in, so presumably that holds the current size of the array. Just use that. Get rid of n.

Another problem is that you are trying to access i when it is not in scope. If you want to access it after the for loop you need to declare it before the loop:

1
2
3
4
5
6
7
8
9
int i = 0;
for ( ; i < num_patients && data[i] != x; ++i)
    ;
if (i < num_patients)
{
    --num_patients;
    for (int j = i; j < num_patients; ++j)
        data[j] = data[j + 1];
}

> int n=sizeof(data)/sizeof(data[0]);
This only works when you have the real array in scope. All you have is a pointer, so it's wrong.
Also, you have the real array length in the num_patients reference.

> if(data[i]==x)
data is a structure, so perhaps
if ( data[i].id ==x )


Even for a small snippet, your indentation sucks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void dodelete(patient_info data[], int &num_patients)
{
  int x;
  cout << "Enter Patient id ";
  cin >> x;
  int n = sizeof(data) / sizeof(data[0]);
  for (int i = 0; i < num_patients; i++)
    if (data[i] == x)
      break;
  if (i < n) {
    n = n - 1;
    for (int j = i; j < num_patients; j++)
      data[j] = data[j + 1];
    cout << "patient record deleted";
  } else
    cout << "Invalid choice";
}

Now it becomes more obvious that the if on line 10 is trying to use the value of i from the previous for loop. The problem is, that variable has gone out of scope - it's restricted to the loop.

Try something like this, which deletes inside the loop, so no scope issues.
1
2
3
4
5
6
7
8
9
10
11
12
13
void dodelete(patient_info data[], int &num_patients)
{
  int x;
  cout << "Enter Patient id ";
  cin >> x;
  for (int i = 0; i < num_patients; i++)
    if (data[i].id == x) {
      for (int j = i+1; j < num_patients; j++)
        data[j-1] = data[j];
      cout << "patient record deleted";
      num_patients--;
    }
}


And keep a tight grip on your indentation.
Let me start by saying that I agree with Salem on this, but if your professor won't let you do things the better way... oh well. I tried to clean up some of your code for you. Here is what I got so far...

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<iostream>
#include<string>
#include<conio.h>
#include<windows.h>
#include<time.h>
//#pragma comment(lib, "user32")
using namespace std;

struct patient_info {
	int id;
	char name[22];
	char adress[200];
	char contact[12];
	char cnic[13];
	int age;
	char sex[8];
	char blood_gp[5];
	char disease_past[50];
};

////////////////////////////Function Declaration//////////////////////////////
void login();
void header();
void footer();
void lpd();
void load();
void menu(int&);
void gotoxy(short x, short y);
bool compareCnic(char[], char[]);

////////////////////////////////////Main Function/////////////////////////////////////////
int main() {
	int numberOfpatients = 0;

	login();
	menu(numberOfpatients);
	return 0;
}

bool compareCnic(char a[], char b[]) {
	bool result = true;
	for (int i = 0; i < 13; i++) {
		if (a[i] != b[i]) {
			result = false;
		}
	}
	return result;
}


void login() { //login
	::SendMessage(::GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x20000000);
	system("color 37");
	//	textbackground(3);
	string username = "";
	string pass = "";
	char ch;
	header();
	cout << "\n\n\n\n\t\t\t\t\t\t\t\t  HOSPITAL MANAGEMENT SYSTEM \n\n";
	cout << "\t\t\t\t\t\t\t\t------------------------------";
	cout << "\n\t\t\t\t\t\t\t\t\t     LOGIN \n";
	cout << "\t\t\t\t\t\t\t\t------------------------------\n\n";
	cout << "\t\t\t\t\t\t\t\t\fEnter Username:";
	cin >> username;
	cout << "\t\t\t\t\t\t\t\t\fEnter Password:";
	ch = _getch();
	while (ch != 13) { //character 13 is enter
		pass.push_back(ch);
		cout << '*';
		ch = _getch();
	}
	if (username == "atta" && pass == "5521") {
		cout << "\n\t\t\t\t\t\t\t\tAccess Granted!\t\fLOADING \n\t";

		cout << "   \t\t\t\t\t\t";
		load();
	}
	else {
		cout << "\n\n\t\t\t\t\t\t\t\t\aAccess Aborted...\n\t\t\t\t\t\t\t\tPlease Try Again\n\n";
		system("pause");
		::SendMessage(::GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x20000000);
		system("CLS");
		login();
	}
}

void lpd() {
	system("cls");
	cout << "\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
	cout << "\t\t\t\t\t@@ _______________________________________________________________________________________ @@\n";
	cout << "\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout << "\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout << "\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout << "\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout << "\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout << "\t\t\t\t\t@@|                                           		                                  |@@\n";
	cout << "\t\t\t\t\t@@|                                  WELCOME TO                                           |@@\n";
	cout << "\t\t\t\t\t@@|                                                                                       |@@\n";
	cout << "\t\t\t\t\t@@|                           HOSPITAL MANAGEMENT SYSTEM                                  |@@\n";
	cout << "\t\t\t\t\t@@|                                                                                       |@@\n";
	cout << "\t\t\t\t\t@@|                                                                                       |@@\n";
	cout << "\t\t\t\t\t@@|                                                                                       |@@\n";
	cout << "\t\t\t\t\t@@|                                                                                       |@@\n";
	cout << "\t\t\t\t\t@@|                                                                                       |@@\n";
	cout << "\t\t\t\t\t@@|                                                 -Brought To You by Atta ul Munim      |@@\n";
	cout << "\t\t\t\t\t@@|_______________________________________________________________________________________|@@\n";
	cout << "\t\t\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n\n\n\t\t\t\t\t";

}
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
void header() {
	time_t time_ptr;
	time_ptr = time(NULL);

	// Get the localtime 
	tm* tm_local = localtime(&time_ptr);

	cout << "\n\n\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tTIME::" << tm_local->tm_hour << ":"
		<< tm_local->tm_min << ":"
		<< tm_local->tm_sec;
}

void footer() {
	cout << "\n\n\t\t\t\t Developed By::\n \t\t\t\t\t\t Atta UL Munim" << endl;
}

void load() {
	for (int i = 0; i < 12; i++) {
		cout << "\xdd\xdd\xdd";
		Sleep(100);
	}
	system("cls");
}
void gotoxy(short x, short y) {         //definition of gotoxy function//
	COORD pos = { x,y };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void menu(int &numberOfpatients) {
	lpd();
	system("pause");
	system("cls");
	patient_info data[10];
	int num;
	int n;
	int b = 0;
	int option;
	int i;
	char sr[13];
	cout << "\n\n\n\t\t\t\t\t\t\t\t  HOSPITAL MANAGEMENT SYSTEM \n";
b:
	cout << "\n\n\t\t\t\t\t\tPlease,  Choose from the following Options: \n\n";
	cout << "\t\t\t\t\t\t _________________________________________________________________ \n";
	cout << "\t\t\t\t\t\t|                                           	                  |\n";
	cout << "\t\t\t\t\t\t|             1  >> Add New Patient Record                        |\n";
	cout << "\t\t\t\t\t\t|             2  >> Search For A Patint                           |\n";
	cout << "\t\t\t\t\t\t|             3  >> Delete Patient Record                         |\n";
	cout << "\t\t\t\t\t\t|             4  >> Display All Admitted Patients                 |\n";
	cout << "\t\t\t\t\t\t|             5  >> Exit the Program                              |\n";
	cout << "\t\t\t\t\t\t|_________________________________________________________________|\n\n";

a:
	cout << "\t\t\t\t\t\tEnter your choice--> ";
	cin >> i;
	system("cls");
	if (i > 5 || i < 1) {
		cout << "\n\n\n\t\t\t\t\t\t\t\t\aInvalid choice!";
		goto b;
	}

	if (i == 1) {
		char c;
		do {
				system("cls");
				cout << "\t\t\t\t\t\t\t--------------------------------------------" << endl;
				cout << "\t\t\t\t\t\t\t|         Enter Patient Details            |" << endl;
				cout << "\t\t\t\t\t\t\t--------------------------------------------" << endl;
				cout << "\n\t\t\t\t\t\t\t      ID :: ";
				cin.ignore();
				cin >> data[numberOfpatients].id;
				cout << "\n\t\t\t\t\t\t\t    Name :: ";
				cin.ignore();
				cin >> data[numberOfpatients].name;
				cout << "\n\t\t\t\t\t\t\t Address ::";
				cin.ignore();
				cin >> data[numberOfpatients].adress;
				cout << "\n\t\t\t\t\t\t\t Contact ::";
				cin.ignore();
				cin >> data[numberOfpatients].contact;
				cout << "\n\t\t\t\t\t\t\tCNIC/NTN :: ";
				cin.ignore();
				cin >> data[numberOfpatients].cnic;
				cout << "\n\t\t\t\t\t\t\t     Age :: ";
				cin.ignore();
				cin >> data[numberOfpatients].age;
				cout << "\n\t\t\t\t\t\t\t     Sex :: ";
				cin.ignore();
				cin >> data[numberOfpatients].sex;
				cout << "\n\t\t\t\t\t\t\tBlood Group:: ";
				cin.ignore();
				cin >> data[numberOfpatients].blood_gp;
				cout << "\n\t\t\t\t\t\t\tPast Disease(if any) :: ";
				cin.ignore();
				cin >> data[numberOfpatients].disease_past;
				cout << "\n\t\t\t\t\t\t\t\aPatient added successfully!.\n";
				cout << "\n\t\t\t\t\t\t\tPress Y to Add aonother patient/Press N to go to main menu\n";
				cin.ignore();
				numberOfpatients++;
		} while (_getch() == 'y' || _getch() == 'Y');
		{
			system("cls");
			goto a;
		}

		goto b;
	}


	if (i == 2 && numberOfpatients > 0) {
	c:
		cout << "\t\t\t\t\t\t\t------------------------" << endl;
		cout << "\t\t\t\t\t\t\t|  Search For A Patint  |" << endl;
		cout << "\t\t\t\t\t\t\t|-----------------------|" << endl;
		cout << "\t\t\t\t\t\t\t|1 >> Search by ID      |" << endl;
		cout << "\t\t\t\t\t\t\t|2 >> Search by CNIC/NTN|" << endl;
		cout << "\t\t\t\t\t\t\t------------------------" << endl;
		cout << "\t\t\t\t\t\t\tEnter Your Choice-->";
		cin >> option;

		switch (option) {
		case 1: {
			int k = -1;
			cout << "\t\t\t\tEnter Patieint id::";
			cin >> num;
			for (int i = 0; i < numberOfpatients; i++)
				if (data[i].id == num)
					k = i;

			if (k == -1) {
				cout << "\n\t\t\t\tInvalid ID!";
			}
			else {
				cout << "\n\t\t\t\tPatient info\n";
				cout << "\n\t\t\t\t**************************\n";
				cout << "\n\t\t\t\t        ID ::" << data[k].id << endl;
				cout << "\n\t\t\t\t      Name ::" << data[k].name << endl;
				cout << "\n\t\t\t\t   Address ::" << data[k].adress << endl;
				cout << "\n\t\t\t\t   Contact ::" << data[k].contact << endl;
				cout << "\n\t\t\t\t  CNIC/NTN ::" << data[k].cnic << endl;
				cout << "\n\t\t\t\t       Age ::" << data[k].age << endl;
				cout << "\n\t\t\t\t       Sex ::" << data[k].sex << endl;
				cout << "\n\t\t\t\tBlood Group ::" << data[k].blood_gp << endl;
				cout << "\n\t\t\t\tPast Disease::" << data[k].disease_past << endl;
				cout << "\n\t\t\t\t**************************\n";
				system("pause");
				system("cls");
				goto b;
			}
		}
				break;
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
case 2: {
			int k = -1;
			cout << "\t\t\t\t\tSearch By Patient CNIC/NTN\n";
			cout << "\t\t\t\t\tEnter Patient CNIC/NTN-->";
			cin >> sr;
			for (int i = 0; i < numberOfpatients; i++) {
				if (compareCnic(data[i].cnic, sr)) {
					k = i;
				}
			}
			if (k == -1) {
				cout << "\n\t\t\t\t\tInvalid CNIC/NTN";
			}
			else {
				cout << "\n\t\t\t\tPatient info\n";
				cout << "\n\t\t\t\t**************************\n";
				cout << "\n\t\t\t\t        ID ::" << data[k].id << endl;
				cout << "\n\t\t\t\t      Name ::" << data[k].name << endl;
				cout << "\n\t\t\t\t   Address ::" << data[k].adress << endl;
				cout << "\n\t\t\t\t   Contact ::" << data[k].contact << endl;
				cout << "\n\t\t\t\t  CNIC/NTN ::" << data[k].cnic << endl;
				cout << "\n\t\t\t\t       Age ::" << data[k].age << endl;
				cout << "\n\t\t\t\t       Sex ::" << data[k].sex << endl;
				cout << "\n\t\t\t\tBlood Group ::" << data[k].blood_gp << endl;
				cout << "\n\t\t\t\tPast Disease::" << data[k].disease_past << endl;
				cout << "\n\t\t\t\t**************************\n";
				system("pause");
				system("cls");
				goto b;
			}
			break;
		}
		default:
			cout << "Invalid Choice!\a";
			goto c;
		}
	}

	if (i == 3 && numberOfpatients > 0) {
		system("cls");
		cout << "DELETE PATIENT RECORD\n";
		goto a;
	}

	if (i == 4) {
		cout << "DISPLAY ALL ADMITTED PATIENTS\n";
		goto b;
	}

	if (i == 5) {
		cout << "Press R to run the program again.\t Press any key to exit. ";
		if (_getch() == 'r' || _getch() == 'R') {
			system("cls");
			login();
		}
		else {
			footer();
		}
	}

	else {
		goto b;
	}
}
thanks for all the help .project is almost completed.
i wish i ll come again.
by the way what is actual c++ , once salem c wrote that i should use actual c++.
what is actual c++

He probably means that you should use std::string instead of raw char arrays and that you shouldn't use C functions like gets (which was deprecated in C++11 and removed in C++14). And you shouldn't say f(void) to indicate that f takes no arguments. In C++, we just say f().

Also, things like system("cls") and getch() mean that people not running Windows can't run your program. But that's your choice.
then what are other codes to clear screean and pause the program, probably we need to use these codes so what should i do instead of cls and pause .
suggest me a book for the clearance of each topic of c++, i have a local book of it series but it is not good enough.
Last edited on
Pages: 12