-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchBook.c
166 lines (119 loc) · 4.18 KB
/
searchBook.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
#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 searchBook() {
SetConsoleTitle("Library System > Book Menu > Search Book");
void choices() {
system("cls");
printf("Search Book By :\n\n");
printf("Book Title\t(T)\n");
printf("Author Name\t(A)\n");
printf("ISBN\t\t(I)\n");
printf("Category\t(C)\n");
}
do {
char choice[20];
char choiceTitle[50];
char choiceAuthor[50];
char choiceISBN[50];
char choiceCategory[20];
bool enteredTitle = false;
bool enteredAuthor = false;
bool enteredISBN = false;
bool enteredCategory = 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]=='T' || choice[i]=='A' || choice[i]=='I' || choice[i]=='C' || choice[i]=='+' ) {
// title
if( choice[i]=='T' ) {
printf("\nTitle : ");
strupr(gets(choiceTitle));
enteredTitle = true;
enteredCount++;
}
// author
else if( choice[i]=='A' ) {
printf("\nAuthor : ");
strupr(gets(choiceAuthor));
enteredAuthor = true;
enteredCount++;
}
// ISBN
else if( choice[i]=='I' ) {
printf("\nISBN : ");
strupr(gets(choiceISBN));
enteredISBN = true;
enteredCount++;
}
// category
else if( choice[i]=='C' ) {
printf("\nCategory : ");
strupr(gets(choiceCategory));
enteredCategory = true;
enteredCount++;
}
}
else {
errMsg("Error!\nusage: enter one of the given choices");
break;
}
}
} while ( i<(strlen(choice)-1) );
// declaring a variable of bookDataType so if found, it can be used
// no need for array
// writing into files will be instantly
bookDataType book;
FILE * books = fopen("books.bin", "r");
// failure check
if(books==NULL) {
perror("\a\nError");
getche();
return;
}
// read from original file
while( fread(&book,sizeof(bookDataType),1,books) ) {
int specsCount = 0;
if(enteredTitle)
if( strcmp( strupr(book.title) , choiceTitle)==0 )
specsCount++;
if(enteredAuthor)
if( strcmp( strupr(book.author) , choiceAuthor)==0 )
specsCount++;
if(enteredISBN)
if( strcmp( strupr(book.ISBN) , choiceISBN)==0 )
specsCount++;
if(enteredCategory)
if( strcmp( strupr(book.category) , choiceCategory)==0 )
specsCount++;
if(specsCount == enteredCount){
found = true;
showBookDetails(book.ISBN);
}
}
fclose(books);
if(!found)
errMsg("No such specifications for a book");
} while ( yesNoRequest("search another book")==true );
fflush(stdin);
bookMenu();
}