This is the last task I need to submit to pass the unit, and I cannot for the life of my figure out what I'm doing. The task is first year basic C++. The program is a Knight Database with a menu and...

This is the last task I need to submit to pass the unit, and I cannot for the life of my figure out what I'm doing. The task is first year basic C++. The program is a Knight Database with a menu and simple functions to essentially add knight details, delete knight details, update knight details etc. I have attached the task sheet as well as my existing code - my existing code has foundation components that need to remain as is (was an in class walk through, and have now been made to expand on that). This is using SplashKit - and has been written in Visual Studio Code.


#include "splashkit.h" #include #include using std::vector; using namespace std; struct knight_data { string name; string month; int age; int year; }; struct kingdom_data { string name; vector knights; }; enum knight_update_option { UPDATE_NAME, UPDATE_AGE, UPDATE_BIRTH_MONTH, UPDATE_BIRTH_YEAR, FINISH_UPDATE }; enum kingdom_update_option { ADD_KNIGHT, QUERY_KNIGHT, DELETE_KNIGHT, UPDATE_KNIGHT, KINGDOM_DETAILS, QUIT }; string read_string(string prompt) { string result; write(prompt); result = read_line(); return result; } int read_integer(string prompt) { string line; line = read_string(prompt); return convert_to_integer(line); } knight_data read_knight() { knight_data result; result.name = read_string("Enter name: "); result.age = read_integer("Enter age: "); result.month = read_string("Enter birth month: "); result.year = read_integer("Enter year of birth: "); return result; } void write_knight(const knight_data &knight) { write_line(knight.name + " aged " + to_string(knight.age) + " born in " + (knight.month) + " " + to_string(knight.year)); } 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 Year of Birth"); write_line("5: Finish Update"); result = read_integer("Select option: "); return static_cast(result - 1); } 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_BIRTH_MONTH: knight.month = read_string("Enter new birth month: "); break; case UPDATE_BIRTH_YEAR: knight.year = read_integer("Enter new year of birth: "); break; case FINISH_UPDATE: break; default: write_line("Please select a valid option"); } } while ( option != FINISH_UPDATE ); } kingdom_update_option read_kingdom_update_option() { int result; write_line("1: Add Knight"); write_line("2: Query Knight"); write_line("3: Delete Knight"); write_line("4: Update Knight"); write_line("5: Kingdom Details"); write_line("6: Quit"); result = read_integer("Select option: "); return static_cast(result - 1); } int select_knight(const kingdom_data &kingdom) { int selected_index; do { for(auto i = 0U; i < kingdom.knights.size();="" i++)="" {="" write_line(to_string(i="" +="" 1)="" +="" ":="" "="" +="" kingdom.knights[i].name);="" }="" write_line("="" ");="" write_line("which="" numbered="" knight="" would="" you="" like="" to="" select?="" (type="" 0="" to="" return="" to="" the="" menu)="" ");="" selected_index="convert_to_integer(read_line());" while="" (selected_index="">< 0="" or="" selected_index=""> (int)kingdom.knights.size()) { write_line("Please Select A Valid Knight Number."); selected_index = convert_to_integer(read_line()); } if(selected_index > 0) { write_line("yay:)"); } else { write_line("oh no"); } return selected_index - 1; } while (selected_index != 0); } void query_knight(kingdom_data &kingdom) { int selected_knight; select_knight(kingdom); selected_knight = select_knight(kingdom); write_knight(kingdom.knights[selected_knight]); } void add_knight(kingdom_data &kingdom) { knight_data new_knight; new_knight = read_knight(); kingdom.knights.push_back(new_knight); } void delete_knight(kingdom_data &kingdom) { int selected_knight; select_knight(kingdom); selected_knight = select_knight(kingdom); if ( selected_knight != -1 ) { delete_knight(kingdom); } } void delete_last_knight(kingdom_data &kingdom, size_t 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();="" }="" }="" void="" write_kingdom(const="" kingdom_data="" &kingdom)="" {="" write_line("="================");" write_line("="" "="" +="" kingdom.name="" +="" "="" ");="" write_line("="================");" write_line();="" write_line("="===" knights="===");" for(auto="" i="0U;" i="">< kingdom.knights.size(); i++) { write_knight(kingdom.knights[i]); } write_line("================="); } kingdom_data read_kingdom() { kingdom_data result; result.name = read_string("enter kingdom name: "); return result; } void update_kingdom(kingdom_data &kingdom) { kingdom_update_option option; do { write_line(); write_line("== menu =="); write_line(); option = read_kingdom_update_option(); switch(option) { case add_knight: add_knight(kingdom); break; case query_knight: query_knight(kingdom); break; case delete_knight: delete_knight(kingdom); break; case update_knight: update_knight(kingdom.knights); break; case kingdom_details: write_kingdom(kingdom); break; case quit: break; default: write_line("please select a valid option"); } } while ( option != quit ); } int main() { kingdom_data my_kingdom = read_kingdom(); update_kingdom(my_kingdom); return 0; } 7.1p arrays and structs in this task you will continue to work on the knight database to help camelot keep track of all of their knights. we can now add a kingdom struct to help work with and manage all of the knights in the kingdom. use the instructions on the following pages to create a small program that explores a more complex use of arrays and structures. submit the following files to ontrack. your program code a screen shot of your program running answers to the accompanying questions the focus of this task is on the tools and approaches to working with more complex data using structs and dynamic arrays, with matching functions and procedures to help ease the coding efforts. sit102 introduction to programming pass task 7.1: arrays and structs overview submission details to get started: 1. watch this week’s video on updating the kingdom.knights.size();="" i++)="" {="" write_knight(kingdom.knights[i]);="" }="" write_line("="================");" }="" kingdom_data="" read_kingdom()="" {="" kingdom_data="" result;="" result.name="read_string("Enter" kingdom="" name:="" ");="" return="" result;="" }="" void="" update_kingdom(kingdom_data="" &kingdom)="" {="" kingdom_update_option="" option;="" do="" {="" write_line();="" write_line("="=" menu="=");" write_line();="" option="read_kingdom_update_option();" switch(option)="" {="" case="" add_knight:="" add_knight(kingdom);="" break;="" case="" query_knight:="" query_knight(kingdom);="" break;="" case="" delete_knight:="" delete_knight(kingdom);="" break;="" case="" update_knight:="" update_knight(kingdom.knights);="" break;="" case="" kingdom_details:="" write_kingdom(kingdom);="" break;="" case="" quit:="" break;="" default:="" write_line("please="" select="" a="" valid="" option");="" }="" }="" while="" (="" option="" !="QUIT" );="" }="" int="" main()="" {="" kingdom_data="" my_kingdom="read_kingdom();" update_kingdom(my_kingdom);="" return="" 0;="" }="" 7.1p="" arrays="" and="" structs="" in="" this="" task="" you="" will="" continue="" to="" work="" on="" the="" knight="" database="" to="" help="" camelot="" keep="" track="" of="" all="" of="" their="" knights.="" we="" can="" now="" add="" a="" kingdom="" struct="" to="" help="" work="" with="" and="" manage="" all="" of="" the="" knights="" in="" the="" kingdom.="" use="" the="" instructions="" on="" the="" following="" pages="" to="" create="" a="" small="" program="" that="" explores="" a="" more="" complex="" use="" of="" arrays="" and="" structures.="" submit="" the="" following="" files="" to="" ontrack.="" your="" program="" code="" a="" screen="" shot="" of="" your="" program="" running="" answers="" to="" the="" accompanying="" questions="" the="" focus="" of="" this="" task="" is="" on="" the="" tools="" and="" approaches="" to="" working="" with="" more="" complex="" data="" using="" structs="" and="" dynamic="" arrays,="" with="" matching="" functions="" and="" procedures="" to="" help="" ease="" the="" coding="" efforts.="" sit102="" introduction="" to="" programming="" pass="" task="" 7.1:="" arrays="" and="" structs="" overview="" submission="" details="" to="" get="" started:="" 1.="" watch="" this="" week’s="" video="" on="" updating="">
May 31, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here