Repository navigation
Expand file tree
/
Copy pathprogram.cpp
More file actions
368 lines (304 loc) · 8.38 KB
/
Copy pathprogram.cpp
File metadata and controls
368 lines (304 loc) · 8.38 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
#include "splashkit.h"
#include "terminal_user_input.h"
#include <string>
#include <vector>
#include <iostream>
using std::vector;
using namespace std;
//creates a struct called knight data
struct knight_data
{
string name;
int age;
string month;
string colour;
vector<string> tools;
};
struct kingdom_data
{
string name;
vector<knight_data> knights;
};
//reads the data into the struct
knight_data read_knight()
{
knight_data result;
result.name = read_string("Enter name: ");
result.age = read_integer("Enter age: ");
result.month = read_string("Enter a birth month: ");
result.colour = read_string("Enter their favourite colour: ");
return result;
}
//writes out the knights details to the terminal using information via pass by reference
//updated to include the read out of the tools
void write_knight(const knight_data &knight)
{
write_line(knight.name + " aged " + to_string(knight.age) + " born in " + knight.month + " whose favourite colour is " + knight.colour);
for(int i = 0; i < knight.tools.size(); i++)
{
write_line(knight.tools[i]);
}
return;
}
//creates an enum for the updated knight options
//extended to include the add and delete tools functions
enum knight_update_option
{
UPDATE_NAME,
UPDATE_AGE,
UPDATE_MONTH,
UPDATE_COLOUR,
ADD_TOOL,
DELETE_TOOL,
FINISH_UPDATE
};
//displays the menu for updating the knight data
//extended to include the add and delete tool update function
knight_update_option read_knight_update_option()
{
int result;
write_line("1: Update name");
write_line("2: Update age");
write_line("3: Update birth month");
write_line("4: Update favourite colour");
write_line("5: Add Tool");
write_line("6: Delete Tool");
write_line("7: Finish update");
result = read_integer("Select option: ");
return static_cast<knight_update_option>(result - 1);
}
//this procedure takes the tool selected by the user and deletes it from the dynamic array
//It works within Knight data only and is only accessible using the update knight menu
void delete_tool(knight_data &knight, int index)
{
if ( index >= 0 and index < knight.tools.size() )
{
int last_idx;
last_idx = knight.tools.size() - 1;
knight.tools[index] = knight.tools[last_idx];
knight.tools.pop_back();
}
}
//this procedure adds a tool to a knight in a dynamic array
//the user is able to continue adding tools until they press "q" for quit
//Tools are entered as strings, and converted to uppercase
void add_tool(knight_data &knight)
{
bool finish = false;
string tool;
do
{
tool = to_uppercase(read_string("Enter tool to add, or Q to finish adding tools: "));
if(tool == "Q")
{
finish = true;
}
else
{
knight.tools.push_back(tool);
}
} while (finish != true);
return;
}
//This allows the user to select the tool they wish to delete
//If no tools exist, it outputs an error message
//Sends selected tool to be deleted
void select_tool(knight_data &knight)
{
int j;
int option;
if (knight.tools.size() == 0)
{
write_line("No tool is found in record");
}
else
{
for(int i = 0; i < knight.tools.size(); i++)
{
j=i+1;
write(to_string(j) + ": ");
write_line(knight.tools[i]);
}
option = read_integer("Select your tool: ");
delete_tool(knight, option - 1);
}
return;
}
//Allows the user to select a knight to be updated.
//Uses kingdom data
//Error checks by ensuring a valid option is entered
int select_knight(const kingdom_data &kingdom)
{
int option, j;
bool valid = false;
for(int i = 0; i < kingdom.knights.size(); i++)
{
j=i+1;
write(to_string(j) + ": ");
write_knight(kingdom.knights[i]);
}
do
{
option = read_integer("Select your knight: ");
if (option < 0 or option > kingdom.knights.size())
{
write_line("Please enter a valid option");
}
else
{
valid = true;
}
}while(valid != true);
return option - 1;
}
//procedure which asks the user to enter new values and updates the knight data using information via pass by reference
//updated to include add and delete tools
void update_knight(knight_data &knight)
{
knight_update_option option;
do
{
write_line();
write_line("== Update Knight ==");
write_knight(knight);
write_line();
option = read_knight_update_option();
switch(option)
{
case UPDATE_NAME:
knight.name = read_string("Enter new name: ");
break;
case UPDATE_AGE:
knight.age = read_integer("Enter new age: ");
break;
case UPDATE_MONTH:
knight.month = read_string("Enter new birth month: ");
break;
case UPDATE_COLOUR:
knight.colour = read_string("Enter new favourite colour: ");
break;
case ADD_TOOL:
add_tool(knight);
break;
case DELETE_TOOL:
select_tool(knight);
break;
case FINISH_UPDATE:
break;
default:
write_line("Please select a valid option");
}
} while (option != FINISH_UPDATE);
}
//Allows a user to add a new knight to the kingdom
void add_knight(kingdom_data &kingdom)
{
knight_data new_knight;
new_knight = read_knight();
kingdom.knights.push_back(new_knight);
}
//outputs kingdom details
void write_kingdom(const kingdom_data &kingdom)
{
write_line("=================");
write_line(kingdom.name);
write_line("==== Knights ====");
for(int i = 0; i < kingdom.knights.size(); i++)
{
write_knight(kingdom.knights[i]);
}
write_line("=================");
return;
}
//deletes a knight from the kingdom
void delete_knight(kingdom_data &kingdom, int index)
{
if ( index >= 0 and index < kingdom.knights.size() )
{
int last_idx;
last_idx = kingdom.knights.size() - 1;
kingdom.knights[index] = kingdom.knights[last_idx];
kingdom.knights.pop_back();
}
}
//Asks the user to enter a kingdom name
kingdom_data read_kingdom()
{
kingdom_data result;
result.name = read_string("Enter kingdom name: ");
return result;
}
//Gets the user to select a knight to query and outputs the information for that knight
void query_knight(const kingdom_data &kingdom)
{
int i;
i = select_knight(kingdom);
write_knight(kingdom.knights[i]);
return;
}
//deletes a knight from the kingdom
void delete_selection(kingdom_data &kingdom)
{
int i;
i = select_knight(kingdom);
if ( i > 0)
{
write_knight(kingdom.knights[i]);
}
write_line("About to delete...");
delete_knight(kingdom, i);
return;
}
//allows the user to select a knight to update from the kingdom data
void mod_knight(kingdom_data &kingdom)
{
int option;
option = select_knight(kingdom);
update_knight(kingdom.knights[option]);
return;
}
//update kingdom menu
void update_kingdom(kingdom_data &kingdom)
{
int option;
do
{
write_line("==== Menu ====");
write_line("1: Add a Knight");
write_line("2: Delete a Knight");
write_line("3: Display Kingdom Details");
write_line("4: Query a Knight");
write_line("5: Update a Knight");
write_line("6: Quit");
option = read_integer("Please Select an Option: ");
switch(option)
{
case 1:
add_knight(kingdom);
break;
case 2:
delete_selection(kingdom);
break;
case 3:
write_kingdom(kingdom);
break;
case 4:
query_knight(kingdom);
break;
case 5:
mod_knight(kingdom);
break;
case 6:
break;
default:
write_line("Please select a valid option");
}
} while (option != 6);
return;
}
int main()
{
kingdom_data my_kingdom = read_kingdom();
update_kingdom(my_kingdom);
return 0;
}