-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
132 lines (127 loc) · 4.6 KB
/
main.cpp
File metadata and controls
132 lines (127 loc) · 4.6 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
#include "main.h"
// MAIN
int main() {
welcome(); // always print welcome message
ifstream inFile; // for reading from farms.txt
FarmList farmList;
char option;
if (!openInFile(inFile)) { // atempt to open file
cout << "Error opening file!! Terminating..." << endl;
return 1;
}
readFromFile(inFile, farmList); // populate farms[] with data and close file
option = getMenuChoice(); // get the first menu choice(validated)
while (option != 'q') { // continute menu loop until user enters q for quit
execCommand(option, farmList);
option = getMenuChoice();
}
ofstream outFile; // for writing to farms.txt
if (!openOutFile(outFile)) { // attempt to open file
cout << "Error opening file!! Terminating..." << endl;
return 1;
}
readToFile(farmList, outFile); // copy adjusted arrays to file in same format
cout << "Writing to Farms.txt..." << endl;
cout << "Thank you for using the local farm database!" << endl;
return 0;
}
// function definitions
void welcome() {
cout << "Welcome!\nThis program will help you manage the local farms." << endl << endl;
}
// this function attempts to open the farms.txt file for reading
bool openInFile(ifstream& inFile) {
inFile.open("farms.txt");
if (inFile.fail()) {
return false;
}
return true;
}
// this function reads from inFile to populate the farms[] array, assumed inFile was previously opened.
void readFromFile(ifstream& inFile, FarmList &farmList) {
while (inFile.peek() != EOF) { // read until end of file
Farm tempFarm;
char tempName[MAX_CHAR];
char tempLocation[MAX_CHAR];
int tempRating, tempNumProducts;
int tempProduct; // changes as go through array, assigned to products[i] using setProductAt(tempProduct, i)
inFile.getline(tempName, MAX_CHAR, ';');
tempFarm.setName(tempName);
inFile.getline(tempLocation, MAX_CHAR, ';');
tempFarm.setLocation(tempLocation);
inFile >> tempRating;
tempFarm.setRating(tempRating);
inFile.ignore(); // ignore the ';'
inFile >> tempNumProducts;
tempFarm.setNumProducts(tempNumProducts);
inFile.ignore(); // ignore the ';'
for (int i = 0; i < tempFarm.getNumProducts(); i++) {
inFile >> tempProduct; // read the product code for that index
tempFarm.setProductAt(tempProduct, i); // set the product code at index i
inFile.ignore(); // ignore the ','
}
inFile.ignore(10000, '\n');
farmList.insertFarm(tempFarm); // insert the current farm into the list
}
inFile.close(); // closes inFile at the end of the loop
cout << "Successfully read from file!" << endl << endl;
}
// this function prints the menu and gets a valid menu choice from the user
char getMenuChoice() {
char choice;
cout << MENU << endl;
cin >> choice;
while (!cin || (tolower(choice) != 'q' && (tolower(choice) < 'a' || tolower(choice) > 'e'))) {
cin.clear(); // clear the error flag
cin.ignore(100, '\n'); // clear input buffer
cout << "Invalid input!! Please enter a valid option (a-e or q): ";
cin >> choice;
}
cin.ignore(100, '\n'); // ignore excess buffer
return choice;
}
// this function executes the command based on the menu choice
void execCommand(char option, FarmList &farmList) {
switch (option) {
case 'a':
farmList.addFarm();
break;
case 'b':
farmList.listFarms();
break;
case 'c':
farmList.searchLocation();
break;
case 'd':
farmList.removeByIndex();
break;
case 'e':
farmList.searchByProduct();
break;
default:
exit(1); // option has been previously validated in getMenuChoice(), but if something goes wrong this exits
}
}
// this function attempts to open the farms.txt file for writing, returns fail if fail to open file
bool openOutFile(ofstream& outFile) {
outFile.open("farms.txt");
if (!outFile.is_open()) {
return false;
}
return true;
}
// this function writes the farms[] array to the farms.txt file in the same format as it was read back to farms.txt
void readToFile(FarmList farmList, ofstream& outFile) {
// assume file was previously opened with openOutFile()
for (int i = 0; i < farmList.getCount(); i++) { // for each farm
outFile << farmList.getFarmAt(i).getName() << ";"; // name
outFile << farmList.getFarmAt(i).getLocation() << ";"; // location
outFile << farmList.getFarmAt(i).getRating() << ";"; // rating
outFile << farmList.getFarmAt(i).getNumProducts() << ";"; // numProducts
for (int j = 0; j < farmList.getFarmAt(i).getNumProducts(); j++) {
outFile << farmList.getFarmAt(i).getProductAt(j) << ","; // products (seperated by commas)
}
outFile << endl; // new line between farm entries
}
outFile.close(); // closes the output file
}