Order Now  +1 678 648 4277 

Updating Sheldon Cooper’s Enemies List in C++ Assignment Help

Sheldon Cooper has asked you to update his enemies list program. He wants to make it more free form so that you can enter more information about the fiends that appear. It should allow multiple fields per enemy, but when it displays them it should line up neatly with the header and details aligned. Below is the solution provided by our C++ assignment helpers.

Updating Lists in Object-Oriented Programming

#include #include #include #include #include #include #include #include using namespace std; // returns true when alpahabetical key is passed bool valid_key(string key) { return isalpha(key[0]); } /* prints the keyvalue in the map 'm' according to the order in the 'keysorder' vector while preserving the number of the spaces to be equal to 'max-each key size' where max is the maximum length of the keys in the map */ void printMap(map &m,int max, vector &keysOrder) { max++; for (unsigned int i = 0; i < keysOrder.size(); i++) { cout << keysOrder[i]; for (unsigned int j = 0; j < max - static_cast(keysOrder[i].size()); j++) cout << " "; cout << m[keysOrder[i]] << endl; } } /* adds the key to the map and the vector return 0 in success return -1 when invalid key is encountered return -2 when duplicate key is encountered */ int addKeyValue(map &m, string& k, string& v, int &keyMaxSize,vector &keysOrder) { if (!valid_key(k)) return -1; if (m.find(k) != m.end()) { return -2; } keyMaxSize = max(keyMaxSize, static_cast(k.size())); m[k] = v; keysOrder.push_back(k); return 0; } // trims the input from any trailing taps or spaces void trim(string &s) { if (s.empty()) return; int i = 0; while (s[i] == ' ' || s[i] == '\r' || s[i] == '\t') i++; int j = s.size() - 1; while (s[j] == ' ' || s[j] == '\r' || s[j] == '\t') j--; s = s.substr(i, j-i+1); } /* split the 's' to key and value according to the description "Each line starts with an alphabetic key, followed by some spaces, followed by a value." */ void split(string& s,vector&v) { if (s.empty()) return; int index = s.find(" "); int index2 = index; if (index == -1) { v.push_back(s); return; } else { while (s[index2+1] == ' ') index2++; v.push_back(s.substr(0, index)); v.push_back(s.substr(index2 + 1)); } } int main(int argc,char*argv[]) { (void)argc; // just to avoid the warning of unused variable map m; // map to store key and value after validation string line; // line read from input vector keyValueVector; // key value directly read from line vector keysOrder; // vector, preserves the order of the keys in the input file int keyMaxSize = -1; // keeps traail of the longest key so spaces adapts to int res; // result from adding the key to the key value map int lineNumber = 0; while (true) { lineNumber++; keyValueVector.clear(); if (!getline(cin, line)) { // end of file encounterd printMap(m, keyMaxSize,keysOrder); if(m.size() != 0) cout << endl; break; } trim(line); split(line, keyValueVector); if (keyValueVector.size() == 0) { // end line // next enemy encounterd printMap(m,keyMaxSize,keysOrder); if (m.size() != 0) cout << endl; m.clear(); keysOrder.clear(); keyMaxSize = -1; continue; } else if (keyValueVector.size() != 2) { // invalid input cerr << argv[0] << " has encountered an error: key or value is missing in line "<