C++ not using classes Create a function named ReadCities, which takes a string i
ID: 3858393 • Letter: C
Question
C++ not using classes
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.
Explanation / Answer
//Test c++ program for ReadCities method
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <iomanip>
#include <limits.h>
using namespace std;
/**
The function ReadCities that takes two string file names and read data from
inFile and writes to output file outFile if the no error in file opening of input
file inFile otherwise retunr -1 .
*/
int ReadCities(string inFile, string outFile)
{
//Open intput file
ifstream fin(inFile);
//checking if file doesnot exist
if(!fin)
{
return -1;
}
//open output file
ofstream fout("output.txt");
string line;
int maxElevation = 0;
string maxCity;
int Largest = 0;
string largeCity;
//read data into line utnil end of file
while(getline(fin,line))
{
//set line to stringstream
stringstream linestream(line);
//declarea variables
string city;
string state;
string population;
string elevation;
std::string::size_type sz;
//get city from linestream separated by comma
getline(linestream,city,',');
//get state from linestream separated by comma
getline(linestream,state,',');
//get population from linestream separated by comma
getline(linestream,population,',');
//get elevation from linestream separated by comma
getline(linestream,elevation,',');
if(atoi(elevation.c_str()) > maxElevation)
{
maxElevation = atoi(elevation.c_str());
maxCity = city;
}
if(atoi(population.c_str()) > Largest)
{
Largest = atoi(population.c_str());
largeCity = city;
}
}
fout << "Highest: " << maxCity << "," << maxElevation << endl;
fout << "Largest: " << largeCity << "," << Largest << endl;
}//end of function ReadCities
//main method
int main()
{
//calling method
ReadCities("input.txt", "output.txt");
return 0;
}
/*
output:
input.txt
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
output.txt
Highest: Denver,5883
Largest: New York,8491079
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.