cannot display output

in this code, I need to write and always update the file, I had done this part, but I try to display the output, it only displays the first data. and when I do sorting, the program return with the abnormal return value.

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//sort patient
//search doctor

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#define MAX 20
using namespace std;

/////////////////////////////////////////////////class patient///////////////////////////////////////////////////////////
class Patient
{
	private:
		string name, ic, contact;
		int age, day, month, year;
		bool gender;	//0=female, 1=male
		string doc;
		
	public:
		void setPatient(string name, string ic, string contact, int day, int month, int year, int age, bool gender, string doc)
		{
			this->name=name;
			this->ic=ic;
			this->contact=contact;
			this->day=day;
			this->month=month;
			this->year=year;
			this->age=age;
			this->gender=gender;
			this->doc=doc;
		}
		
		void display()
		{
			cout << name << "\t"
				 << ic << "\t\t"
				 << contact << "\t\t"
				 << day << "-" << month << "-" << year << "\t"
				 << age <<"\t";
				 
			if(gender==false)
			cout << "Female\t\n";
				
			else
			cout << "Male\t\n";
		}
		
		string getName()
		{return name;}
			
		int getAge()
		{return age;}
			
		int getDay()
		{return day;}
		
		int getMonth()
		{return month;}
		
		int getYear()
		{return year;}
		
};

int sortMenu()
{
	int choice;
	cout << "\n<<< Sorting Process >>>\n[1] By name\n[2] By age\n[3] By date appointment\n";
	cout << "\nOption: ";
	cin >> choice;
	
	return choice;
}

/////////////////////////////////////////////////main function///////////////////////////////////////////////////////////
int main()
{
	Doctor dArray[5] = {Doctor("ALI","PSYCHIATRY", "P101",52,"MALE"), 
						Doctor("JESSICA","DENTIST","D101",38,"FEMALE"), 
						Doctor("LEE", "CARDIOLOGY","C101", 47, "MALE"), 
						Doctor("HANIS", "PSYCHIATRY", "P202", 45, "FEMALE"),
						Doctor("PATHENA", "CARDIOLOGY", "C202", 50, "FEMALE")};
	
	Patient pArray[MAX];
	string pName, pIC, pContact, dID;
	int pNum=0, choice, choice2, pDay, pMonth, pYear, pAge;
	char sc;
	bool pGender;
	
	do{
		choice=menu();
		
		switch(choice)
		{
			case 1:	//patient system
			{
				cout << "\n=============	PATIENT MENU	=============\n"
				 	 << "\t1. DISPLAY DOCTORS' LIST\n\t2. SEARCH DOCTOR\n\t3. BOOK AN APPOINTMENT\n\t4. BACK TO MAIN MENU\n"
				  	 << "CHOICE: ";
				cin >> choice2;
				
				do
				{
					switch(choice2)
					{
						case 1:		//display doctor list
						{
							cout << "\n=================== DOCTOR LIST ====================\n"
							 	 << "NAME" << setw(14) << "FIELD" << setw(12) << "ID" << setw(12) << "AGE" << setw(14) << "GENDER\n";
							 
							for(int i=0; i<5; ++i)
							dArray[i].Doctor_display();
						
							cout << "\nNUMBER OF DOCTOR: 5\n\n";
						
							break;
						}
				
						case 2:		//search doctor
						{
								{
									cout << "\nSort doctor list by ID:\n";	
									cout<<"Please enter the doctor's ID (In CAPITAL letter): ";
									cin>>searchkey;
								}
						
								else
								{
									cout<<"\nInvalid Choice!\n";
									system("pause");
									break;
								}
							
								result = searchDoctor(searchChoice,searchkey, dArray, 5);

							}while(result == 1);
							
							break;
						}
						
						case 3://Book appointment and print patient into output file
						{
							cout << "Enter your name: ";
							cin.ignore();
							getline(cin, pName);
							cout << "Enter your IC number (XXXXXX-XX-XXXX): ";
							getline(cin, pIC);
							cout << "Enter your contact number (XXX-XXXXXXX): ";
							getline(cin, pContact);
							cout << "Enter your date for appointment (DD space MM space YYYY): ";
							cin >> pDay >> pMonth >> pYear;
							cout << "Enter your age: ";
							cin >> pAge;
							cout << "Enter gender (0 for female, 1 for male): ";
							cin >> pGender;
							cout << "Enter your doctor's ID to appoint: ";
							cin.ignore();
							getline(cin, dID);	
							
							//write into output file
							fstream outfile;
							outfile.open("appointment.txt", ios::out | ios::app);
							outfile << pName << "\t"
									<< pIC << "\t"
									<< pContact << "\t"
									<< pDay << "\t"
									<< pMonth << "\t"
									<< pYear << "\t"
									<< pAge << "\t"
									<< pGender << "\t"
									<< dID << "\n"; 
							pNum++;	//no proble here
							cout << pNum;
							break;
						}
						
						case 4://Back to main menu
						{
							cout<<"Back to main menu\n";
							system("CLS");
							break;
						}
						
						default:
						{
							cout<<"\nInvalid choice!"<<endl;
							system("pause");
							system("CLS");
							break;
						}
					}
				
				cout << "\n=============	PATIENT MENU	=============\n"
					 << "\t1. DISPLAY DOCTORS' LIST\n\t2. SEARCH DOCTOR\n\t3. BOOK AN APPOINTMENT\n\t4. BACK TO MAIN MENU\n"
				  	 << "CHOICE: ";
				cin >> choice2;
				
				}while(choice2!=3);	
				break;
			} 
		
			case 2:	//admin system
			{
				int choice3;
				do
				{
					system("CLS");
					cout << "=============	ADMIN MENU	=============\n"
						 <<"\t1. DISPLAY PATIENTS' LIST \n\t2. SORT PATIENTS\n\t3. BACK TO MAIN MENU\n"
						 <<"CHOICE: ";
					cin >> choice3;
							
					ifstream infile;
					infile.open("appointment.txt");
								
					if(!infile)
					{
						//print error message
						cout<<"Error! Failed to open the file"<<endl;
						exit(1);//terminate the program as fail to open the file
					}
					while(!infile.eof() && infile>>pName>>pIC>>pContact>>pDay>>pMonth>>pYear>>pAge>>pGender>>dID)
					{
						pArray[pNum].setPatient(pName, pIC, pContact, pDay, pMonth, pYear, pAge, pGender, dID);
						pNum++;	
						cout << pNum;
					}		
							
					switch(choice3)
					{
						case 1:		//display patient list
						{
							if(pNum==0)
							{
								cout << "There is no patient in the list yet!\n";
							}
							else
							{
								cout << "\n=============	PATIENT LIST	=============\n"
									 << "NAME" << setw(14) << "IC NO." << setw(23) << "CONTACT" << setw(26) << "DATE BOOKED"
									 << setw(8) <<"AGE" << setw(12) << "GENDER\n";
									 
								for(int i=0; i<pNum; ++i)
								pArray[i].display();
								
								cout << "\nNUMBER OF PATIENTS: " << pNum << "\n\n";
							}
								
							system("pause");
							break;
						}
								
						case 2:		//sort patient
						{
							int sortChoice, cont2=1; 
							char sc;
							
							if(pNum==0)
							{
								cout << "There is no patient in the list yet!\n";
							}
							else
							{
								cout << "\nEnter 'a' for ascending sorting, 'd' for descending sorting: ";
								cin >> sc;
								
								do{
									sortChoice=sortMenu();
									
									if(sortChoice==1)
									cout << "\nSort patient list by name:\n";
									
									else if(sortChoice==2)
									cout << "\nSort patient list by age:\n";
									
									else if(sortChoice==3)
									cout << "\nSort patient list by date appointment:\n";
									
									else
									{
										cout<<"\nInvalid Choice! Please enter your choice again\n";
										continue;
									}
													
									for(int i=0; i<pNum; i++)
									sort(sc, sortChoice, pArray, pNum-1);	
										
									cout << "NAME" << setw(14) << "IC NO." << setw(23) << "CONTACT" << setw(26) << "DATE BOOKED"
										 << setw(8) <<"AGE" << setw(12) << "GENDER\n";
									
									for (int i = 0; i < pNum; ++i)
									pArray[i].display();
									
									cout << "\nDO YOU WANT TO CONTINUE TO SORT PATIENTS? ( 1-YES, 0-NO ) :";
									cin >> cont2;
									
								}while(cont2==1);
							}
							break;
						}	
						
						case 3://Back to main menu
						{
							cout<<"Back to main menu\n";
							system("CLS");
							break;
							
						}
								
						default:
						{
							cout<<"Invalid choice!"<<endl;
							system("pause");
							break;
						}
					}
				}while(choice2!=3);	
				break;
			}
			
			case 3: 
			{
				cout<<"THANK YOU! BYE BYE."<<endl;
				exit(0);
			}
			
			default:
			{
				cout<<"INVALID CHOICE."<<endl;
				break;	
			}
		}
		}while(choice!=3); 
		
	
	return 0;
}
The first thing I would suggest you do is break up your 250+ line main into some functions.

switch/cases get long in a hurry, and having them nested inside one another in a single function is just a mess.

Each of your menu actions in each case should be a separate 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

int patientMenu(void) {
    cout << "Display/Search/Book/back" << endl;
    int choice;
    cin >> choice;
    return choice;
}

void doPatientMenu(Doctor dArray[], Patient pArray[]) {
    int choice;
    do {
        choice = patientMenu();
        switch(choice) {
            case 1:
                doListDoctors(dArray);  //!! you implement this
                break;
            case 2:
                doSearchDoctors(dArray);
                break;
        }
    } while(choice != 4);
}

int topMenu(void) {
    cout << "Patient/admin/quit" << endl;
    int choice;
    cin >> choice;
    return choice;
}

int main()
{
    Doctor dArray[5] = {Doctor("ALI","PSYCHIATRY", "P101",52,"MALE"), 
                        Doctor("JESSICA","DENTIST","D101",38,"FEMALE"), 
                        Doctor("LEE", "CARDIOLOGY","C101", 47, "MALE"), 
                        Doctor("HANIS", "PSYCHIATRY", "P202", 45, "FEMALE"),
                        Doctor("PATHENA", "CARDIOLOGY", "C202", 50, "FEMALE")};
    Patient pArray[MAX];
    int choice;
    do {
        choice = topMenu();
        switch(choice) {
            case 1:
                doPatientMenu(dArray,pArray);
                break;
            case 2:
                doAdminMenu(dArray,pArray);
                break;
        }
    } while(choice != 3);
}
 


If you had a single function to read the file (and only read the file), you would be able to answer very specific questions like "does reading the file work?".
will work on it! thank you!
Topic archived. No new replies allowed.