1081 程式設計

題目說明

製作一簡單的KTV預約系統。

  1. Login: Input an ID number and password. If the ID number does not exist in the file Members.dat or the password is wrong, then login fails.
    1. Make Reservation: Insert a new record into the binary file ReservationInfo.dat.
    2. Reservation Enquiry/Canceling: Display all records in the file eservationInfo.dat for the current user, then let the user choose one of them to delete.
  2. Registration: Insert a new record into the binary file Members.dat. Input an ID number, name and password. If the ID number already exists in the file Members.dat, then registration fails.

The file record format of the file Members.dat is

1
2
3
char IDNumber[ 12 ];	// account number
char password[ 24 ]; // password
char name[ 8 ]; // name

The file record format of the file ReservationInfo.dat is

1
2
3
4
char IDNumber[ 12 ];	// account number
int branchCode; // branch code
Date date; // reservation date
int numCustomers; // number of customers

Caskbox KTV
branchCode Branch names
1 Taipei Dunhua South
2 Taipei SOGO
3 Taipei Songjiang
4 Taipei Nanjing
5 Taipei Linsen
6 Taipei Zhonghua New
7 Banqiao Guanqian
8 Yonghe Lehua
9 Taoyuan Zhonghua
10 Taoyuan Nankan
11 Zhongli Zhongyang
12 Hsinchu Beida
13 Taichung Ziyou
14 Chiayi Ren’ai
15 Tainan Ximen
16 Kaohsiung Zhonghua New

Input & Output 範例

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
Welcome to the Cashbox Party World!

1 - Login
2 - Registration
3 - End

Enter your choice (1~3): g

Enter your choice (1~3): 0

Enter your choice (1~3): 4

Enter your choice (1~3): 2

Input your ID Number: 555

The ID number 555 is illegal.

1 - Login
2 - Registration
3 - End

Enter your choice (1~3): 2

Input your ID Number: ggg

The ID number ggg is illegal.

1 - Login
2 - Registration
3 - End

Enter your choice (1~3): 2

Input your ID Number: g66

The ID number g66 is illegal.

1 - Login
2 - Registration
3 - End

Enter your choice (1~3): 2

Input your ID Number: c120057214

The ID number c120057214 is illegal.

1 - Login
2 - Registration
3 - End

Enter your choice (1~3): 2

Input your ID Number: c120057213
Input your Name: james
Choose a password: 111

Registration Completed!

1 - Login
2 - Registration
3 - End

Enter your choice (1~3): 2

Input your ID Number: c120057213

You are already a member!

1 - Login
2 - Registration
3 - End

Enter your choice (1~3): 1

Please enter your ID number: c120057222
Enter your password: 111

Invalid account number or password. Please try again.

Please enter your ID number: c120057213
Enter your password: 222

Invalid account number or password. Please try again.

Please enter your ID number: c120057213
Enter your password: 111

1 - Make Reservation
2 - Reservation Enquiry/Canceling
3 - End

Enter your choice (1~3): a

Enter your choice (1~3): 0

Enter your choice (1~3): 4

Enter your choice (1~3): 1

1. Taipei Dunhua South
2. Taipei SOGO
3. Taipei Songjiang
4. Taipei Nanjing
5. Taipei Linsen
6. Taipei Zhonghua New
7. Banqiao Guanqian
8. Yonghe Lehua
9. Taoyuan Zhonghua
10. Taoyuan Nankan
11. Zhongli Zhongyang
12. Hsinchu Beida
13. Taichung Ziyou
14. Chiayi Renai
15. Tainan Ximen
16. Kaohsiung Zhonghua New

Enter your choice (0 to end): a

Enter your choice (0 to end): 17

Enter your choice (0 to end): 9

The current hour is 2019/11/24:14

Available days:

1. 2019/11/24
2. 2019/11/25
3. 2019/11/26
4. 2019/11/27
5. 2019/11/28
6. 2019/11/29
7. 2019/11/30

Enter your choice (0 to end): a

Enter your choice (0 to end): 8

Enter your choice (0 to end): 1

Enter hour (15~23): a

Enter hour (15~23): 14

Enter hour (15~23): 24

Enter hour (15~23): 15

Enter the number of customers (1~30, 0 to end): a

Enter the number of customers (1~30, 0 to end): 31

Enter the number of customers (1~30, 0 to end): 5

Branch Date Hour No of Customers
Taoyuan Zhonghua 2019-11-24 15 5

Reservation Completed!

1 - Make Reservation
2 - Reservation Enquiry/Canceling
3 - End

Enter your choice (1~3): 2

Branch Date Hour No of Customers
1. Taoyuan Zhonghua 2019-11-24 15 5

Choose a reservation to cancel (0: keep all reservations): a

Choose a reservation to cancel (0: keep all reservations): 2

Choose a reservation to cancel (0: keep all reservations): 0

1 - Make Reservation
2 - Reservation Enquiry/Canceling
3 - End

Enter your choice (1~3): 3

1 - Login
2 - Registration
3 - End

Enter your choice (1~3): 3

Thank you! Goodbye!

請按任意鍵繼續 . . .

參考解法

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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <ctime>
using namespace::std;

struct Date
{
int year;
int month;
int day;
int hour;
};

struct MemberRecord
{
char IDNumber[12]; // account number
char password[24]; // password
char name[8]; // name
};

struct ReservationRecord
{
char IDNumber[12]; // account number
int branchCode; // branch code
Date date; // reservation date
int numCustomers; // number of customers
};

char branchNames[17][24] = { "", "Taipei Dunhua South", "Taipei SOGO",
"Taipei Songjiang", "Taipei Nanjing",
"Taipei Linsen", "Taipei Zhonghua New",
"Banqiao Guanqian", "Yonghe Lehua",
"Taoyuan Zhonghua", "Taoyuan Nankan",
"Zhongli Zhongyang", "Hsinchu Beida",
"Taichung Ziyou", "Chiayi Ren'ai",
"Tainan Ximen", "Kaohsiung Zhonghua New" };

// input memberDetails from the file Members.dat
void loadMemberDetails(vector< MemberRecord >& memberDetails);

// input reservations from the file Reservations.dat
void loadReservations(vector< ReservationRecord >& reservations);

// input an integer in the range [ begin, end ]
int inputAnInteger(int begin, int end);

// login and display the submenu
void login(const vector< MemberRecord >& memberDetails,
vector< ReservationRecord >& reservations);

// there exists a member with specified IDNumber and password
bool valid(char IDNumber[], char password[],
const vector< MemberRecord >& memberDetails);

// add a new reservation for the member with specified IDnumber
void reservation(char IDNumber[], vector< ReservationRecord >& reservations);

// compute the current date
void compCurrentDate(Date& currentDate);

// compute 7 dates which is starting from the current date
void compAvailableDates(Date currentDate, Date availableDates[]);

// display all fields of reservation
void output(ReservationRecord reservation);

// display all reservations for the member with specified IDnumber,
// then let the member to choose one of them to delete
void queryDelete(char IDNumber[], vector< ReservationRecord >& reservations);

// add a new member
void registration(vector< MemberRecord >& memberDetails);

// return true if IDNumber is a legal ID number
bool legalID(char IDNumber[]);

// return true if IDNumber belongs to memberDetails
bool existingID(char IDNumber[], const vector< MemberRecord >& memberDetails);

// output all memberDetails into the file Members.dat
void saveMemberDetails(const vector< MemberRecord >& memberDetails);

// output all reservations into the file Reservations.dat
void saveReservations(const vector< ReservationRecord >& reservations);

int main()
{
vector< MemberRecord > memberDetails; // member details for all members
vector< ReservationRecord > reservations; // all reservations

loadMemberDetails(memberDetails);
loadReservations(reservations);

cout << "Welcome to the Cashbox Party World!\n\n";

int choice;

while (true)
{
cout << "1 - Login\n";
cout << "2 - Registration\n";
cout << "3 - End\n";

do cout << "\nEnter your choice (1~3): ";
while ((choice = inputAnInteger(1, 3)) == -1);
cout << endl;



switch (choice)
{
case 1:
login(memberDetails, reservations);
break;

case 2:
registration(memberDetails);
break;

case 3:
saveMemberDetails(memberDetails);
saveReservations(reservations);
cout << "Thank you! Goodbye!\n\n";
system("pause");
return 0;

default:
cout << "\nIncorrect choice!\n";
break;
}
}

system("pause");
}

// input memberDetails from the file Members.dat
void loadMemberDetails(vector< MemberRecord >& memberDetails)
{
fstream inFile("Members.dat", ios::in | ios::out | ios::binary);

inFile.seekg(0, ios::end);

// calculus the number of lines
int numLine = inFile.tellg() / sizeof(memberDetails);

// start from the beginning
inFile.seekg(0, ios::beg);

for (int i = 0; i < numLine; i++)
{
memberDetails.resize(numLine);
inFile.read(reinterpret_cast<char*>(&memberDetails[i]), sizeof(memberDetails[i]));
}

inFile.clear();
}

// input reservations from the file Reservations.dat
void loadReservations(vector< ReservationRecord >& reservations)
{
fstream inFile("Reservations.dat", ios::in | ios::out | ios::binary);

inFile.seekg(0, ios::end);

// calculus the number of reservations
int numLine = inFile.tellg() / sizeof(reservations);

// start from the beginning
inFile.seekg(0, ios::beg);

for (int i = 0; i < numLine; i++)
{
reservations.resize(numLine);
inFile.read(reinterpret_cast<char*>(&reservations[i]), sizeof(reservations[i]));
}
}

// input an integer in the range [ begin, end ]
int inputAnInteger(int begin, int end)
{
char choice;

vector<char>temp;

int number = 0;

cin >> choice;


while (1)
{
if (choice == '\n')
{
break;
}

temp.resize(temp.size() + 1);
temp[temp.size() - 1] = choice;
cin.get(choice);
}

//turn character into integer
for (int i = 0; i < temp.size(); i++)
{
number += (temp[i] - '0') * pow(10, (temp.size() - i - 1));
}

// if the option is between BEGIN and END
if (number >= begin && number <= end)
{
return number;
}

else
{
return -1;
}

/*char choice = { 0 };

cin >> choice;

// if the option is between 1 and 3
if (choice >= begin + '0' && choice <= end + '0')
{
return choice - '0';
}

// if the option isn't between 1 and 3
else
{
return -1;
}*/
}

// login and display the submenu
void login(const vector< MemberRecord >& memberDetails,
vector< ReservationRecord >& reservations)
{
MemberRecord temp;
//char id[12] = { 0 };
//char pw[24] = { 0 };

cout << "Please enter your ID number :";
cin >> temp.IDNumber;
cout << endl;

//if the id is legal and exist
if (legalID(temp.IDNumber) == 1 && existingID(temp.IDNumber, memberDetails) == 1)
{
cout << "Enter your password :";
cin >> temp.password;
cout << endl;

// if the password is right
if (valid(temp.IDNumber, temp.password, memberDetails) == 1)
{
cout << "Login !" << endl;

int choice;

while (true)
{
cout << "1 - Make Reservation\n";
cout << "2 - Reservation Enquiry/Canceling\n";
cout << "3 - End\n";

do cout << "\nEnter your choice (1~3): ";
while ((choice = inputAnInteger(1, 3)) == -1);
cout << endl;

switch (choice)
{
case 1:
reservation(temp.IDNumber, reservations);
break;

case 2:
queryDelete(temp.IDNumber, reservations);
break;

case 3:
return;

default:
cout << "\nIncorrect choice!\n";
break;
}
}
}

else
{
cout << "Invalid account number or password. Please try again." << endl;
}
}

else
{
cout << "The ID number " << temp.IDNumber << " is illegal." << endl;
}


}

// there exists a member with specified IDNumber and password
bool valid(char IDNumber[], char password[],
const vector< MemberRecord >& memberDetails)
{
bool exist = true;
int j;

//start from the first data
for (j = 0; j < memberDetails.size(); j++)
{
//start from the first character
for (int i = 0; i < 10; i++)
{
// if the id is wrong then jump to the next data
if (memberDetails[j].IDNumber[i] != IDNumber[i])
{
break;
}

// if the id is right then continue comparing the password
if (i = 9)
{
int m;

for (m = 0; m < 10; m++)
{
// if it is letter or number
if ((memberDetails[j].password[m] > 64 && memberDetails[j].password[m] < 91)
|| (memberDetails[j].password[m] > 47 && memberDetails[j].password[m] < 58))
{
//return false if the password isn't fit with id
if (memberDetails[j].password[m] != password[m])
{
exist = false;
return exist;
break;
}
}

else
{
return exist;
}

}

if (m == 9)
{
return exist;
break;
}

}
}
}

if (j == (memberDetails.size() - 1))
{
exist = false;
return exist;
}
}

// add a new reservation for the member with specified IDnumber
void reservation(char IDNumber[], vector< ReservationRecord >& reservations)
{
ReservationRecord temp;
strcpy_s(temp.IDNumber, IDNumber);

cout << "1. Taipei Dunhua South" << endl
<< "2. Taipei SOGO" << endl
<< "3. Taipei Songjiang" << endl
<< "4. Taipei Nanjing" << endl
<< "5. Taipei Linsen" << endl
<< "6. Taipei Zhonghua New" << endl
<< "7. Banqiao Guanqian" << endl
<< "8. Yonghe Lehua" << endl
<< "9. Taoyuan Zhonghua" << endl
<< "10. Taoyuan Nankan" << endl
<< "11. Zhongli Zhongyang" << endl
<< "12. Hsinchu Beida" << endl
<< "13. Taichung Ziyou" << endl
<< "14. Chiayi Ren'ai" << endl
<< "15. Tainan Ximen" << endl
<< "16. Kaohsiung Zhonghua New" << endl;

cout << endl;

int choice;

//enter the choice if the choice is not legal
do cout << "\nEnter your choice (0 to end): ";
while ((choice = inputAnInteger(0, 16)) == -1);

//return if enter 0
if (choice == 0)
{
return;
}

temp.branchCode = choice;

//declaration of seven structures
Date day[7];

//compute current date
Date currentDate;
compCurrentDate(currentDate);

//cout the current date
cout << "\nThe current hour is " << currentDate.year << '/'
<< currentDate.month << '/' << currentDate.day << ':' << currentDate.hour << endl;

//determine the available days
cout << "Available days:" << endl;
cout << endl;
compAvailableDates(currentDate, day);

for (int i = 0; i < 7; i++)
{
cout << (i + 1) << ". "
<< day[i].year << "/"
<< day[i].month << "/"
<< day[i].day << endl;
}

cout << endl;


//choose the day
int daychoice;

//enter the choice if the choice is not legal
do cout << "\nEnter your choice (0 to end): ";
while ((daychoice = inputAnInteger(0, 7)) == -1);

//return if enter 0
if (daychoice == 0)
{
return;
}

temp.date = day[daychoice - 1];

//choose the hour
int hourchoice = currentDate.hour;

//enter the choice if the choice is not legal
do
{
hourchoice = currentDate.hour;
cout << "\nEnter hour (" << hourchoice << "~23): ";
}
while ((hourchoice = inputAnInteger(hourchoice, 23)) == -1);

temp.date.hour = hourchoice;

//choose the number of customers
int numcust;

//enter the choice if the choice is not legal
do cout << "\nEnter the number of customers(1~30, 0 to end) : ";
while ((numcust = inputAnInteger(0, 30)) == -1);

//return if enter 0
if (numcust == 0)
{
return;
}

temp.numCustomers = numcust;

cout << endl;

/*int n = 8;
if (temp.date.day < 10)
n++;*/

cout << " Branch Date Hour No of Customer" << endl;
cout << setw(26) << right << branchNames[choice];
cout << setw(8) << right << day[daychoice - 1].year
<< "-" << day[daychoice - 1].month << "-" << day[daychoice - 1].day;
cout << setw(8) << right << hourchoice;
cout << setw(18) << right << numcust << endl;
cout << endl;
cout << endl;
cout << "Reservation Completed!" << endl;
cout << endl;

reservations.resize(reservations.size() + 1);
reservations[reservations.size() - 1] = temp;
}

// compute the current date
void compCurrentDate(Date& currentDate)
{
int second = static_cast<int>(time(0)) + 8 * 60 * 60;
int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int secondperyear = 365 * 24 * 60 * 60;

currentDate.year = 1970;
currentDate.month = 1;

// determine whether 1970 is a leap year or not
if ((currentDate.year % 4 == 0 && currentDate.year % 100 != 0) || (currentDate.year % 400 == 0))
{
secondperyear += 24 * 60 * 60;
}

//determine how many seconds of leap is are and the current year
while (second >= secondperyear)
{
second -= secondperyear;
currentDate.year++;

secondperyear = 365 * 24 * 60 * 60;

if ((currentDate.year % 4 == 0 && currentDate.year % 100 != 0) || (currentDate.year % 400 == 0))
{
secondperyear += 24 * 60 * 60;
}
}

//set feb = 29 days if it's leap year
if ((currentDate.year % 4 == 0 && currentDate.year % 100 != 0) || (currentDate.year % 400 == 0))
{
days[2] = 29;
}

//determine the month
while (second >= days[currentDate.month] * 24 * 60 * 60)
{
second -= days[currentDate.month] * 24 * 60 * 60;
currentDate.month++;
}

//determine the day
currentDate.day = second / (24 * 60 * 60) + 1;
second %= 24 * 60 * 60;

currentDate.hour = second / (60 * 60);
second %= (60 * 60);
}

// compute 7 dates which is starting from the current date
void compAvailableDates(Date currentDate, Date availableDates[])
{
int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// determine whether the year is leap year or not
if ((currentDate.year % 4 == 0 && currentDate.year % 100 != 0) || (currentDate.year % 400 == 0))
{
days[2] = 29;
}

int year = currentDate.year;
int month = currentDate.month;
int day = currentDate.day;



for (int i = 0; i < 7; i++)
{
availableDates[i].year = year;
availableDates[i].month = month;
availableDates[i].day = day;

//if current date is the last day of the month
if (day == days[month])
{
month++;
day = 1;
}

//if current date is 12/31
else if (month == 12 && day == 31)
{
year++;
month = 1;
day = 1;
}

else
{
day++;
}
}

}

// display all fields of reservation
void output(ReservationRecord reservation)
{
/*int n = 8;
if (reservation.date.day < 10)
n++;*/

cout << setw(27) << right << branchNames[reservation.branchCode];
cout << setw(8) << right << reservation.date.year
<< "-" << reservation.date.month << "-" << reservation.date.day;
cout << setw(8) << right << reservation.date.hour;
cout << setw(18) << right << reservation.numCustomers << endl;

}

// display all reservations for the member with specified IDnumber,
// then let the member to choose one of them to delete
void queryDelete(char IDNumber[], vector< ReservationRecord >& reservations)
{
vector<int> IDposition;

cout << setw(30) << "Brach" << setw(14) << "Date" << setw(8) << "Hour" << setw(19) << "No of Customers" << endl;


for (int i = 0; i < reservations.size(); i++)
{
if (strcmp(reservations[i].IDNumber, IDNumber) == 0)
{
cout << IDposition.size() + 1 << ". ";
output(reservations[i]);
IDposition.resize(IDposition.size() + 1);
IDposition[IDposition.size() - 1] = i;
}
}

//delete the reservation
int choice;

do cout << "\nChoose a reservation to cancel (0: keep all reservations): ";
while ((choice = inputAnInteger(0, IDposition.size())) == -1);

if (choice == 0)
{
return;
}

reservations.erase(reservations.begin() + IDposition[choice - 1]);

//cout << " Branch Date Hour No of Customers" << endl;

cout << endl;
cout << "Reservation deleted!" << endl;
cout << endl;

}

// add a new member
void registration(vector< MemberRecord >& memberDetails)
{
char id[12] = { 0 };

cout << "Input your ID number:";
cin >> id;
cout << endl;

//if the id isn't legal
if (legalID(id) == 0)
{
cout << "The ID number " << id << " is illegal." << endl;
}

else
{
bool member = false;

//start from the first data
for (int i = 0; i < memberDetails.size(); i++)
{
// if the id is already exist cout "you are already a member"
if (strcmp(id, memberDetails[i].IDNumber) == 0)
{
cout << "You are already a member!" << endl;
member = true;
break;
}
}

if (member == false)
{
char name[8] = { 0 };
char pw[34] = { 0 };

cout << "Input your name:";
cin >> name;
cout << endl;
cout << "Choose a password:";
cin >> pw;

//save the data into structure temp
struct MemberRecord temp;

strcpy_s(temp.IDNumber, id);
strcpy_s(temp.password, pw);
strcpy_s(temp.name, name);

memberDetails.push_back(temp);

cout << "Registration Completed!" << endl;
}
}
}

// return true if IDNumber is a legal ID number
bool legalID(char IDNumber[])
{
int local = 0;
int idnum[26] = { 10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33 };

//turn the first letter into number
for (int i = 0; i < 26; i++)
{
if (IDNumber[0] == (65 + i))
{
local = idnum[i];
}

}

int total = 0;

total = (local % 10) * 9 + (local / 10) + (IDNumber[1] - '0') * 8
+ (IDNumber[2] - '0') * 7 + (IDNumber[3] - '0') * 6 + (IDNumber[4] - '0') * 5
+ (IDNumber[5] - '0') * 4 + (IDNumber[6] - '0') * 3 + (IDNumber[7] - '0') * 2 + (IDNumber[8] - '0') * 1;

//return true if the id accrods with standards
if ((IDNumber[9] - '0') == total % 10 || (IDNumber[9] - '0') == (10 - (total % 10)))
{
return true;
}

else
{
return false;
}
}

// return true if IDNumber belongs to memberDetails
bool existingID(char IDNumber[], const vector< MemberRecord >& memberDetails)
{
bool exist = true;
int j;

//start from the first data
for (j = 0; j < memberDetails.size(); j++)
{
for (int i = 0; i < 10; i++)
{
//if the id isn't identical, jump to the next data
if (memberDetails[j].IDNumber[i] != IDNumber[i])
{
break;
}

//return true if the id is identical
if (i = 9)
{
return exist;
break;
}
}
}

if (j == (memberDetails.size() - 1))
{
exist = false;
return exist;
}
}

// output all memberDetails into the file Members.dat
void saveMemberDetails(const vector< MemberRecord >& memberDetails)
{
fstream inFile("Members.dat", ios::in | ios::out | ios::binary);

for (int i = 0; i < memberDetails.size(); i++)
{
inFile.write(reinterpret_cast<const char*>(&memberDetails[i]), sizeof(memberDetails[i]));
}

inFile.clear();
}

// output all reservations into the file Reservations.dat
void saveReservations(const vector< ReservationRecord >& reservations)
{
fstream inFile("Members.dat", ios::in | ios::out | ios::binary);

for (int i = 0; i < reservations.size(); i++)
{
inFile.write(reinterpret_cast<const char*>(&reservations[i]), sizeof(reservations[i]));
}

inFile.clear();
}