Create a function named ReadCities, which takes a string input filename and stri
ID: 3861938 • Letter: C
Question
Create a function named ReadCities, which takes a string input filename and string output filename as a parameters. This function returns the number of cities read from the file. If the input file cannot be opened, return -1 and do not print anything to the output file. Read each line from the given filename, parse the data, process the data, and print the required information to the output file. Each line of the file contains CITY STATE, POPULATION, ELEVATION. Read the data and print the city with the largest population and the city with the highest elevation. If given the data below Seattle, Wash 668342, 429 Denver, Colo., 663862, 5883 Washington DC, 658893 16 Indianapolis, Ind., 848788, 797 New York, N.Y., 8491079 13 Los Angeles Calif., 3928864, 126 Your output file should contain: Largest New York City. 8491079 Highest Denver, 5883 You only need to write the function code. Our code will create and test your class. AnswerExplanation / Answer
/*Read Cities functions:
1. Take line by line from a file, and process it, by separating it on the basis of the delimiter as comma and print the largest ones
2. atoi is to convert char array to integer
3. string.c_str() convert the string into a character array
*/
#include <bits/stdc++.h>
using namespace std;
int ReadCities(string f1,string f2)
{
string line;
ifstream file1(f1.c_str());
string city="";
string state="";
string temp="";
long long population;
int elevation;
int i;
int count=0;
string maxpcity="",maxpstate="",maxelcity="";
long long maxpopl=-1;
int maxelevation=-1;
if (file1.is_open())
{
while ( getline (file1,line))
{
count++;
city="";
state="";
temp="";
i=0;
while(line[i]!=',')
{
city+=line[i];
i++;
}
i++;
// cout << city;
while(line[i]!=',')
{
state+=line[i];
i++;
}
i++;
// cout << state;
while(line[i]!=',')
{
temp+=line[i];
i++;
}
population=atoll(temp.c_str());
temp.clear();
i++;
while(line[i]!=' ' && line[i]!=0)
{
temp+=line[i];
i++;
}
elevation=atoi(temp.c_str());
if(population>maxpopl)
{
maxpopl=population;
maxpstate=state;
maxpcity=city;
}
if(elevation>maxelevation)
{
maxelevation=elevation;
maxelcity=city;
}
}
file1.close();
if(count>0)
{
ofstream file2 (f2.c_str());
if (file2.is_open())
{
file2 << .setprecition(2) << fixed <<"Largest: " << maxpcity << ", " << maxpopl<< " ";
file2 << "Highest: " << maxelcity << ", " << maxelevation << ' ';
file2.close();
}
else cout << "Can't open file";
return count;
}
else
cout << "Empty file";
}
else
return -1;
return count;
}
int main()
{
string f1 , f2;
cin >> f1;
cin >> f2;
ReadCities(f1,f2);
return 0;
}
/* Calc cost function takes filename as parameter and returns the entries counted
1. strtod coverts string to double
2. setprecision to set that only 2 digits are required
3. Most of the code is self explainatory.
4. Take the input, process it, convert numbers to integer or double from string datatype**/
#include <bits/stdc++.h>
using namespace std;
int CalcCost(string f1,string f2)
{
string line;
ifstream file1(f1.c_str());
string product="";
string temp="";
double cost;
int quantity;
int i;
int count=0;
vector< pair <string, double > > totalcost;
if (file1.is_open())
{
while ( getline (file1,line))
{
count++;
product="";
temp="";
i=0;
while(line[i]!=',')
{
product+=line[i];
i++;
}
i++;
while(line[i]!=',')
{
temp+=line[i];
i++;
}
cost=strtod(temp.c_str(),NULL);
temp.clear();
i++;
while(line[i]!=' ' && line[i]!=0)
{
temp+=line[i];
i++;
}
quantity=atoi(temp.c_str());
cout << setprecision(2) << fixed;
totalcost.push_back(make_pair(product,quantity*cost));
}
file1.close();
if(count>0)
{
ofstream file2 (f2.c_str());
if (file2.is_open())
{
for(int j = 0 ; j < count;j++)
{
file2 << setprecision(2) << fixed;
file2 << totalcost[j].first << ": " << totalcost[j].second << endl;
}
file2.close();
}
else cout << "Can't open file";
return count;
}
else
cout << "Empty file";
}
else
return -1;
return count;
}
int main()
{
string f1 , f2;
cin >> f1;
cin >> f2;
CalcCost(f1,f2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.