-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
428 lines (381 loc) · 14.3 KB
/
main.cpp
File metadata and controls
428 lines (381 loc) · 14.3 KB
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
/*
* Copyright 2024 - Howard Community College All rights reserved; Unauthorized duplication prohibited.
* Name: Sai Matukumalli
* Class: CMSY-171
* Instructor: Justin Lieberman
* Program Name: Lab 3
* Program Description: This program reads animals from a file, puts them into our animal database vector, and allowing us to manipulate the data points.
* The program allows us to manipulate the file when animals are added or edited as well.
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <fstream>
using namespace std;
const int MAX_LENGTH = 25;
struct Animal {
char name[MAX_LENGTH];
char species[MAX_LENGTH];
int typeCount;
bool endangered;
};
void printCopyright();
void addAnimals(vector<Animal *> *database, vector<string> species);
void displayAnimals(vector<Animal*> database);
void displayEndangered(vector<Animal*> database);
void searchAnimals(vector<Animal *> animals, const vector<string>& species, fstream &stream);
void selectionSortStrings(vector<Animal*> &animals);
void readAnimal(vector<Animal*>& animals, fstream& stream, int ENDANGERED_COUNT);
void readSpecies(vector<string>& animals, fstream&);
void refreshFile(vector<Animal *>& animals, fstream &stream);
void updateRecordInVector(vector<Animal *> animal, int loc, vector<string> species);
void updateRecordInFile(vector<Animal *> animals, int location, fstream &stream);
void updateEndangered(vector<Animal *> *animals, int ENDANGERED_COUNT);
int main() {
printCopyright();
int chosenOption;
const int ENDANGERED_COUNT = 100;
const int ADD_ANIMALS_MENU_OPTION = 1;
const int DISPLAY_ANIMALS_MENU_OPTION = 2;
const int DISPLAY_ENDANGERED_MENU_OPTION = 3;
const int SEARCH_ANIMALS_MENU_OPTION = 4;
const int QUIT_MENU_OPTION = 5;
const string ANIMAL_RECORD_LOCATION = R"(C:\Users\SaiKM\CLionProjects\Lab3\animal.dat)";
const string SPECIES_RECORD_LOCATION = R"(C:\Users\SaiKM\CLionProjects\Lab3\species.txt)";
fstream animalRecords;
animalRecords.open(ANIMAL_RECORD_LOCATION, fstream::in | fstream::out | fstream::binary);
vector<Animal*> database;
readAnimal(database, animalRecords, ENDANGERED_COUNT);
animalRecords.close();
animalRecords.open(ANIMAL_RECORD_LOCATION, ios::binary | ios::out);
refreshFile(database, animalRecords);
animalRecords.close();
fstream speciesRecords;
speciesRecords.open(SPECIES_RECORD_LOCATION);
vector<string> speciesList;
readSpecies(speciesList, speciesRecords);
const char WELCOME_TEXT[] = "Welcome to Animal Vector Database\n\n";
const char MENU_TEXT[] = "\n1. Add Animal(s)"
"\n2. Display Animals"
"\n3. Display Endangered"
"\n4. Search animals"
"\n5. Quit";
cout << WELCOME_TEXT;
bool quit = true;
while (quit) {
chosenOption = 0;
cout << MENU_TEXT;
cout << "\nEnter a menu choice: ";
cin >> chosenOption;
while (
!(chosenOption == ADD_ANIMALS_MENU_OPTION
|| chosenOption == DISPLAY_ANIMALS_MENU_OPTION
|| chosenOption == DISPLAY_ENDANGERED_MENU_OPTION
|| chosenOption == SEARCH_ANIMALS_MENU_OPTION
|| chosenOption == QUIT_MENU_OPTION)) {
cout << "Invalid option";
cout << MENU_TEXT << endl;
cout << "Enter a menu choice: ";
if (!(cin >> chosenOption)) {
return 1;
}
}
//Exhaustive
switch (chosenOption) { // NOLINT(*-multiway-paths-covered)
case ADD_ANIMALS_MENU_OPTION: {
addAnimals(&database, speciesList);
animalRecords.open(ANIMAL_RECORD_LOCATION, ios::out | ios::binary);
refreshFile(database, animalRecords);
break;
}
case DISPLAY_ANIMALS_MENU_OPTION: {
displayAnimals(database);
break;
}
case DISPLAY_ENDANGERED_MENU_OPTION: {
displayEndangered(database);
break;
}
case SEARCH_ANIMALS_MENU_OPTION: {
animalRecords.open(ANIMAL_RECORD_LOCATION, ios::binary | ios::in | ios::out);
searchAnimals(database, speciesList, animalRecords);
break;
}
case QUIT_MENU_OPTION: {
cout << "Thank you for using animal database";
quit = false;
break;
}
}
}
animalRecords.clear();
speciesRecords.clear();
animalRecords.close();
speciesRecords.close();
while (!database.empty()) {
delete database.back();
database.pop_back();
}
database.clear();
speciesList.clear();
return 0;
}
/*
* Prints Howard CC Copyright statement
*/
void printCopyright() {
cout << "Copyright 2024 - Howard Community College All rights reserved; Unauthorized duplication prohibited\n\n\n\n";
}
/*
* Adds animals to the vector
* Takes input of the animal database, and the list of species
*/
void addAnimals(vector<Animal *> *database, vector<string> species) {
const int ENDANGERED_COUNT = 100;
int animalCount;
string speciesName;
char *inputCstring;
const string NONE = "none";
string type = "Please enter the animal type (none to stop): ";
string speciesText = "Please enter the animal species from the menu";
string number = "Enter the number of animals: ";
string error = "Negative Count, enter a positive value: ";
string alreadyFound = "This animal is already in the database, please enter another animal";
while (true) {
cout << type;
cin.ignore();
string originalCaps;
string input;
bool flag;
do {
flag = false;
getline(cin, input);
originalCaps = input;
for (char &c: input) {
c = tolower(c);
}
if (input == NONE) {
return;
}
inputCstring = new char[originalCaps.length() + 1];
for (int i = 0; i < originalCaps.length(); i++) {
inputCstring[i] = originalCaps.at(i);
}
inputCstring[input.length()] = '\0';\
for(Animal* value : *database){
if((*value).name == originalCaps){
cout << alreadyFound;
flag = true;
}
}
} while (flag);
cout << speciesText << "\n";
for (int i = 0; i < species.size(); ++i) {
cout << i+1 << ". " + species.at(i) << endl;
}
int speciesValue = 0;
cin >> speciesValue;
while(speciesValue <= 0 || speciesValue > species.size()) {
cout << &"Invalid entry, enter a value between 1 and " [ species.size()];
cin >> speciesValue;
}
cout << number;
cin >> animalCount;
while (animalCount < 0) {
cout << error;
cin >> animalCount;
}
cout << "\n";
Animal* animal = new Animal;
animal->typeCount = animalCount;
strncpy_s(animal->name, MAX_LENGTH, inputCstring, MAX_LENGTH);
strncpy_s(animal->species, MAX_LENGTH, speciesName.c_str(), MAX_LENGTH);
animal->endangered = animalCount < ENDANGERED_COUNT;
database->push_back(animal);
selectionSortStrings(*database);
}
}
/*
* Rewrites the entire file in order of the vector
* Takes the vector and the filestream
*/
void refreshFile(vector<Animal *>& animals, fstream &stream) {
stream.seekp(ios::beg);
for (int i = 0; i < animals.size(); i++) {
stream.write(reinterpret_cast<char*>(animals.at(i)), sizeof(Animal));
}
}
/*
* Displays all the animals, including their name, species, count, and endangered status
* Takes the animal database input
*/
void displayAnimals(vector<Animal*> database) {
const char ENDANGERED_STRING[] = "\nThis animal is endangered";
const char NOT_ENDANGERED_STRING[] = "\nThis animal is not endangered";
selectionSortStrings(database);
for (const Animal* ITEM: database) {
cout << "\nAnimal: " << (*ITEM).name <<
"\nOf species " << (*ITEM).species <<
"\nHas a count of: " << (*ITEM).typeCount <<
((*ITEM).endangered ? ENDANGERED_STRING : NOT_ENDANGERED_STRING);
cout << "\n\n";
}
if(database.empty()){
cout << "\nThere are no animals to display\n";
}
}
/*
* Displays only the endangered animals
* Takes input of the database
*/
void displayEndangered(vector<Animal*> database) {
const char ENDANGERED_STRING[] = " is endangered\n";
selectionSortStrings(database);
for(const Animal* VALUE : database){
if((*VALUE).endangered){
cout << (*VALUE).name << ENDANGERED_STRING;
}
}
if(database.empty()){
cout << "\nThere are no endangered animals to display\n";
}
}
/*
* Searches the animals using binary search, and allows the record values to be updated, before rewriting to the file
* Takes input of the animals database, the list of species, and the filestream
*/
void searchAnimals(vector<Animal*> animals, const vector<string>& speciesList, fstream &stream) {
selectionSortStrings(animals);
cout << "Enter the name of the animal you are looking for: ";
int low = 0, mid, high = animals.size()-1;
string input;
cin.ignore();
getline(cin, input);
while (low <= high) {
mid = (low + high) / 2;
Animal currAnimal = *animals.at(mid);
if (strcmp(input.c_str(), currAnimal.name) == 0) {
cout << "Animal Name: " << currAnimal.name
<< "\nAnimal Species: " << currAnimal.species
<< "\nAnimal Count " << currAnimal.typeCount
<< "\nAnimal is " << (currAnimal.endangered ? "endangered" : "not Endangered");
char userChange;
cout << "\n\nDo you want to update the record?";
cin >> userChange;
while(tolower(userChange) != 'y' && tolower(userChange) != 'n'){
cout << "enter a valid value <y or n>";
cin >> userChange;
}
userChange = tolower(userChange);
if(userChange == 'y'){
updateRecordInVector(animals, mid, speciesList);
updateRecordInFile(animals, mid, stream);
}
return;
} else if (strcmp(input.c_str(), currAnimal.name) > 0) {
low = mid + 1;
} else if (strcmp(input.c_str(), currAnimal.name) < 0) {
high = mid - 1;
}
}
cout << "\nThere is no animal entry corresponding to \"" << input << "\"" << endl;
}
/*
* Individually writes animals into the file without clearing it each time
* Takes the database, the location that it needs to be written to, and the filestream
*/
void updateRecordInFile(vector<Animal*> animals, int location, fstream &stream) {
stream.seekg(location * sizeof(Animal), ios::beg);
stream.seekp(ios::beg + location * sizeof(Animal));
stream.write(reinterpret_cast <char*> (animals.at(location)), sizeof(Animal));
stream.close();
}
/*
* Allows individual elements in the vector to be updated, helper method for search
*/
void updateRecordInVector(vector<Animal*> animals, int loc, vector<string> species) {
const string animalUpdate = "Enter the value you want to change animal to(! for no change)";
const string speciesUpdate = "Enter the new species of the animal";
const string countUpdate = "Enter the new count of animals";
string temp;
cout << animalUpdate;
cin.ignore();
getline(cin, temp);
if(temp != "!"){
for (char & specie : animals.at(loc)->species) {
specie = '0';
}
strncpy_s(animals.at(loc)->name, MAX_LENGTH, temp.c_str(), MAX_LENGTH);
}
cout << speciesUpdate << endl;
for (int i = 0; i < species.size(); ++i) {
cout << i+1 << ". " << species.at(i) << endl;
}
int numberInput;
cin >> numberInput;
while(numberInput < 0 || numberInput > species.size()){
cout << "\nThat is an invalid option, enter a value between 0 and " << species.size();
}
for (char & specie : animals.at(loc)->species) {
specie = '0';
}
strncpy_s(animals.at(loc)->species, animals.size(), species.at(numberInput).c_str(), animals.size());
cout << countUpdate;
cin >> numberInput;
while(numberInput < 0){
cout << "Negative Number, please enter a positive numberInput for count";
}
animals.at(loc)->typeCount = numberInput;
}
/*
* Sorts the list of animals using a selection sort in the vector
* Takes the animals vector
*/
void selectionSortStrings(vector<Animal*> &animals) {
for (int i = 0; i < animals.size() - 1; ++i) {
int min_idx = i;
for (int j = i + 1; j < animals.size(); ++j) {
if (strcmp((*animals.at(j)).name, (*animals.at(min_idx)).name) > 0) {
min_idx = j;
}
swap(animals.at(min_idx), animals.at(j));
}
}
}
/*
* Reads the animals out of the file and adds them into the vector, and then sorts the vector
*/
void readAnimal(vector<Animal *> &animals, fstream &stream, const int ENDANGERED_COUNT) {
while (!stream.eof()) {
if(!stream.good() || stream.eof()) {
cout << "read failed";
return;
}
Animal* animal = new Animal;
animals.push_back(animal);
stream.read(reinterpret_cast<char*>(animals.back()), sizeof(Animal));
}
animals.pop_back();
updateEndangered(&animals, ENDANGERED_COUNT);
selectionSortStrings(animals);
}
/*
* Updates whether the animals are endangered in the vector
* Takes input of the animals database, and the number of animals needed to be endangered.
*/
void updateEndangered(vector<Animal *> *animals, const int ENDANGERED_COUNT) {
for (Animal *animal : *animals) {
animal->endangered = animal->typeCount < ENDANGERED_COUNT;
}
}
/*
* Reads the values from the species list and puts them into a vector
* Takes input of the filestream and the vector of strings to put speices names.
*/
void readSpecies(vector<string> &animals, fstream &stream) {
while(!stream.eof()){
char *str = new char[25];
stream.getline(str, 25);
animals.emplace_back(str);
}
}