-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchMember.c
168 lines (120 loc) · 4.27 KB
/
searchMember.c
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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include "book.h"
#include "member.h"
#include "admin.h"
#include "menu.h"
#include "dataTypes.h"
#include "utilities.h"
void searchMember() {
SetConsoleTitle("Library System > Member Menu > Search Member");
void choices() {
system("cls");
printf("Search Member By :\n\n");
printf("First Name\t(N)\n");
printf("ID\t\t(I)\n");
printf("Borrowings\t(B)\n");
printf("Phone Number\t(P)\n");
}
do {
char choice[20];
char choiceName[25];
char choiceID[20];
int choiceBorrowings;
char choicePhone[20];
bool enteredName = false;
bool enteredID = false;
bool enteredBorrowings = false;
bool enteredPhone = false;
int enteredCount;
bool found = false;
// iterator
// do-while loop exit depends on
int i;
do{
choices();
printf("\n\n\nYour choice (add '+' for multiple search) : ");
strupr(gets(choice));
enteredCount = 0;
for( i=0 ; i<strlen(choice) ; i++) {
if( choice[i]=='N' || choice[i]=='I' || choice[i]=='B' || choice[i]=='P' || choice[i]=='+' ) {
// name
if( choice[i]=='N' ) {
printf("\nFirst Name : ");
strupr(gets(choiceName));
enteredName = true;
enteredCount++;
}
// ID
else if( choice[i]=='I' ) {
printf("\nID : ");
strupr(gets(choiceID));
enteredID = true;
enteredCount++;
}
// borrowings
else if( choice[i]=='B' ) {
printf("\nNumber of Borrowings : ");
scanf("%i",&choiceBorrowings);
getc(stdin);
enteredBorrowings = true;
enteredCount++;
}
// phone number
else if( choice[i]=='P' ) {
printf("\nPhone Number : ");
strupr(gets(choicePhone));
enteredPhone = true;
enteredCount++;
}
}
else {
errMsg("Error!\nusage: enter one of the given choices");
break;
}
}
} while ( i<(strlen(choice)-1) );
// declaring a variable of memberDataType so if found, it can be used
// no need for array
// reading from files will be instantly
memberDataType member;
FILE * members = fopen("members.bin", "r");
// failure check
if(members==NULL) {
perror("\a\nError");
getche();
return;
}
// read from original file
while( fread(&member, sizeof(memberDataType), 1, members) ) {
int specsCount = 0;
if(enteredName)
if( strcmp( strupr(member.name.first) , choiceName)==0 )
specsCount++;
if(enteredID)
if( strcmp( strupr(member.ID) , choiceID)==0 )
specsCount++;
if(enteredBorrowings)
if( member.borrowedBooks == choiceBorrowings )
specsCount++;
if(enteredPhone)
if( strcmp( strupr(member.phoneNum) , choicePhone)==0 )
specsCount++;
if(specsCount == enteredCount) {
found = true;
showMemberDetails(member.ID);
}
}
fclose(members);
if(!found)
errMsg("No such specifications for a member");
} while ( yesNoRequest("search another member")==true );
fflush(stdin);
memberMenu();
}