Texts: Question 5: 10 Points
Complete the following program that uses a Text file CitiesPopulation.txt. The file contains an unknown number of names of the cities and their populations. Use two separate arrays city[] and pop[] of size 10 each to read information from the file. Display only names and number of cities whose populations are between 50000 and 75000.
//Preprocess directives
#include <iostream>
#include <fstream>
using namespace std;
int main() {
//declare required variables and array
const int size = 10;
string city[size];
int pop[size];
int count = 0;
//file stream, file opening and validation
ifstream fin("CitiesPopulation.txt");
if (!fin) {
cout << "Error opening file." << endl;
return 1;
}
//reading first record
fin >> city[count] >> pop[count];
while (!fin.eof()) {
count++;
//reading Next record
fin >> city[count] >> pop[count];
}
//end of while
int citiesInRange = 0;
for (int i = 0; i < count; i++) {
if (pop[i] >= 50000 && pop[i] <= 75000) {
citiesInRange++;
}
}
//Display Report
cout << "Number of cities with population between 50000 and 75000 are: " << citiesInRange << endl;
fin.close();
return 0;
}
Sample Output:
City: Ibri, Population: 55000
City: Nizwa, Population: 70000