C++ City Populations ve of this lab is compare the populations of various cities
ID: 3881882 • Letter: C
Question
C++ City Populations ve of this lab is compare the populations of various cities that lie in between and Dayton on I-75. Write a program that produces a bar illustrating the populations Toledo The program should read the name of the city and its population from a file. Have continue this process until the end of file is reached. For each city, your program the should display the name of the city and a bar consisting of one asterisk for every 2,000 mcople. For example, if a city's population is between 18,000 and 19,999, 9 asterisks should anpear in the bar. The data can be found in the pop.txt file located on Blackboard. For a hint on how to read data until the end of file is reached, look at the notes on repetition tructures (part 2) that we covered in class. Also look at the same class notes to review how print out a certain number of asterisks to the screen. The output should be formatted similar to the following: City Populations Perrysburg Bowling Green Findlay Huber Heights*** KEY: ) 2000 people Once finished with the above task, modify your program to allow the user to type in how many people each asterisk represents (for example, typing 5000 would make the bar for Findlay 8 asterisks long instead of 20). Finally, add code to your program to track the two cities in the file with the highest populations and list the results after the previously computed results. You can assume that no two cities have the same population.Explanation / Answer
Hi... Please check below cpp code for city populations from file.
Main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
cout<<" City population ";
cout<<"-------------------------------------------- ";
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
//cout << line << ' ';
istringstream iss(line);
int c = 0;
string s1;
int pop=0;
string s;
while ( getline( iss, s, ' ' ) ) {
if(c==0){
s1 = s.c_str();
}else{
stringstream geek(s);
geek >> pop;
}
c++;
}
int b = pop/2000;
string printstr = "";
for(int i=0;i<b;i++){
printstr+="*";
}
cout<<s1<<" "<<printstr<<" ";
}
myfile.close();
cout<<"----------------------------------------------";
}
else cout << "Unable to open file";
return 0;
}
example.txt
Aliabad 22334
Begumet 34524
Output:
City population
--------------------------------------------
Aliabad ***********
Begumet *****************
----------------------------------------------
Please check the code and let me know any issues. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.