Question
I have to write these two functions in c++. RemoveCountry will take a name of a country as a parameter and delete the country with
I have to write these two functions in c++.
RemoveCountry will take a name of a country as a parameter and delete the country with the matching name from the catalogue. If it does not exist, then the method will ultimately have no impact. Watch out for memory leaks and be sure to update the catalogue correctly.
findCountry will take a country name and return a pointer to the country with the matching name in the catalogue (assuming it exists). If it does not exist, return nullptr.
CountryCatalogue.cpp:
#include "CountryCatalogue.h" #include "Country.h"
CountryCatalogue::CountryCatalogue() { _maxSize = 10; _curSize = 0; _catalogue = new Country * [_maxSize]; }
CountryCatalogue::CountryCatalogue(std::string continentFileName, std::string countryFileName) { //block that opens the files and checks to make sure they can be read //open up the files std::ifstream inFile1; std::ifstream inFile2;
//opening both text files and ensuring that the file is readable to the program inFile1.open(continentFileName); if (!inFile1) { std::cout << "Unable to open file"; exit(1); // terminate with error } inFile2.open(countryFileName); if (!inFile2) { std::cout << "Unable to open file"; exit(1); // terminate with error }
// read the continet file // while there is still stuff to read in the file std::string str;
while (!inFile1.eof()){ std::string Country, Cont;
//reading lines from file and assigning to variables std::getline(inFile1, Country); std::getline(inFile1, Cont);
//mapping to variables read from file _countryContinent.insert(std::pair
//closing file after use inFile1.close();
//creating array _catalogue = new Country * [_curSize + 2];
//resetting size to zero for later itteration _curSize = 0;
// read the country file // while there is still stuff to read in the file while (!inFile2.eof()){ std::string name, POP, AREA; int pop; double area = 0.0;
std::getline(inFile2, name); std::getline(inFile2, POP); std::getline(inFile2, AREA);
if (!POP.empty() && POP[POP.length() - 1] == ' ') { POP.erase(POP.length() - 1); } if (!AREA.empty() && AREA[AREA.length() - 1] == ' ') { AREA.erase(AREA.length() - 1); }
pop = std::stoi(POP); area = std::stod(AREA);
//creating iterator to search through mapped values std::map
//creating empty string variable to store continent std::string cont;
//using value found by iterator to make continent string //ensuring value isn't the end valueof the map if (it != _countryContinent.end()) { cont = it->second; }
//std::cout << name << pop << area << cont << std::endl;
// add the country to the catalogue addCountry(name, pop, area, cont);
} } CountryCatalogue::~CountryCatalogue() {
for (int i = 0; i < _curSize; i++){ delete _catalogue[i]; } delete[] _catalogue; } void CountryCatalogue::printCountryCatalogue() {
std::string s;
for (int i = 0; i < _curSize; i++) { s += _catalogue[i]->to_string() + " "; }
std::cout << _curSize << std::endl; }
void CountryCatalogue::expandCapacity() {
//doubling array size _maxSize = _maxSize * 2;
//creating pointer to new array of new size Country** newCatalogue = new Country * [_maxSize];
//copying old array into new for (int i = 0; i < _curSize; i++) { newCatalogue[i] = _catalogue[i]; }
//deleting old array delete[] _catalogue;
//making _catalogue point to newCatalogue _catalogue = newCatalogue; } void CountryCatalogue::addCountry(std::string name, int pop, double area, std::string continent) { Country* toAdd = new Country(name, pop, area, continent); if (_curSize == _maxSize) { expandCapacity(); } //adding country object to array _catalogue[_curSize] = toAdd; //adding to _curSize for next iteration _curSize++; } void CountryCatalogue::removeCountry(std::string name) { *******help****** } Country* CountryCatalogue::findCountry(std::string name) { *****help******* }
Country.cpp:
#include "Country.h"
Country::Country() { _name = "DEFAULT"; _pop = 0; _area = 0; _continent = "DEFAULT"; }
Country::Country(std::string name, int pop, double area, std::string continent) { _name = name; _pop = pop; _area = area; _continent = continent; // complete me }
Country::~Country() { // No need for anything } std::string Country::getName() { return _name; } int Country::getPopulation() { return _pop; } double Country::getArea() { return _area; } std::string Country::getContinent() { return _continent; } double Country::getPopulationDensity() { double popDensity = 0.0; popDensity = _pop / _area; return popDensity; } void Country::setPopulation(int newPop) { _pop = newPop; } std::string Country::to_string() { std::string s = ""; s = _name + " in " + _continent; return s; } bool Country::equals(Country& other) { if (this->_name == other.getName()) { return true; } else { return false; } }
Thanks for any help
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started