Hey guys, me again. So I have to build a program where I have 4 cities, and the user inputs the population for them. Then my output has to produce a bar chart like so:
City 1: *******
City 2: ***
City 3: ********
City 4: *****
So far my code looks like this, and I feel like I have it, my IDE doesn't show any errors but when I go to run it it tells me there's build errors. I'm not sure what I'm doing wrong.
Line 24 flagged as an error in my MSVS 2017. Also produced an error when I compiled it.
Where is "city" defined and it is not in line 9. That is a local variale that is destroyed at the close of the for loop.
I am thinking that "city" could be defined as a "std::string" array of names that could be used in the second for loop.
And if I read this correctly the 2nd for loop will print 4 * for each city. "CITY_NUMBER" need replaced with "population" divided by some value, Since I do not know values you are using for "population", maybe by 1000 or 10,000. Also you might want to consider making "population" a "double" instead of an "int".
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
void getCity( string &name, int &pop )
{
cout << "Enter city name: "; getline( cin, name );
string dummy;
cout << "Enter population of " << name << ": "; cin >> pop; getline( cin, dummy );
if ( pop <= 0 )
{
cout << "Population must be positive\n";
getCity( name, pop );
}
}
int main()
{
constint CITY_NUMBER = 4;
int population[CITY_NUMBER];
string names[CITY_NUMBER];
for ( int i = 0; i < CITY_NUMBER; i++ )
{
cout << "City " << i + 1 << ":\n";
getCity( names[i], population[i] );
}
double scale = 1.0e-5;
for ( int i = 0; i < CITY_NUMBER; i++ )
{
cout << setw( 20 ) << left << names[i] << " " << string( (int)( scale * population[i] + 0.5 ), '*' ) << '\n';
}
}
City 1:
Enter city name: Manchester
Enter population of Manchester: 554000
City 2:
Enter city name: Liverpool
Enter population of Liverpool: 579000
City 3:
Enter city name: Birmingham
Enter population of Birmingham: 1154000
City 4:
Enter city name: Coventry
Enter population of Coventry: 369000
Manchester ******
Liverpool ******
Birmingham ************
Coventry ****