63.8k views
4 votes
The program first reads integer townCount from input, representing the number of pairs of inputs to be read. Each pair has a string and an integer. One Town object is created for each pair and added to vector townList. If a Town object's population is between 2394 and 3803, both inclusive, call the Town object's Print().

Ex: If the input is:
4 Goshen 2394 Hum 1944 Davids 2197 Bow 3803

1 Answer

1 vote

This program reads the number of towns (townCount) and then iterates through each pair, creating a Town object for each pair and adding it to the townList.

If the population of a town falls within the specified range, it calls the Print() method for that town. Note that you may need to adjust the code based on the specific requirements or format of your input data.

Here's a simple C++ program that achieves this:

#include <iostream>

#include <vector>

#include <string>

class Town {

public:

Town(const std::string& name, int population) : name(name), population(population) {}

void Print() const {

std::cout << "Town: " << name << ", Population: " << population << std::endl;

}

int getPopulation() const {

return population;

}

private:

std::string name;

int population;

};

int main() {

int townCount;

std::cin >> townCount;

std::vector<Town> townList;

for (int i = 0; i < townCount; ++i) {

std::string name;

int population;

std::cin >> name >> population;

Town town(name, population);

townList.push_back(town);

if (population >= 2394 && population <= 3803) {

town.Print();

}

}

return 0;

}

User Infra Stank
by
8.0k points