Create a function named ReadCities , which takes a string input filename and str
ID: 3853759 • 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:
c++
Your output file should contain:
Explanation / Answer
ReadCities.CPP:
______________
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
int ReadCities(char[],char[]);
int main(){
clrscr();
char input[20],output[20];
cout<<"Enter input filename:";
cin>>input;
cout<<"Enter output filename:";
cin>>output;
int result = ReadCities(input,output);
if(result== -1)
cout<<"Failes to open "<<input<<" input file"<<endl;
else
cout<<"data stored in output file"<<endl;
getch();
return 0;
}
int ReadCities(char input[],char output[]){
ifstream infile;
infile.open(input);
if(infile.fail()){
long int largest_population = 0;
char city_large[30] ={''};
int high_elevation = 0;
char city_high[30] ={''};
ofstream outfile;
outfile.open(output);
char CITY[30],STATE[30];
long int POPULATION;
int ELEVATION;
for(int i=0;i<6;i++){
infile>>CITY;
infile>>STATE;
infile>>POPULATION;
infile>>ELEVATION;
if(POPULATION > largest_population){
largest_population = POPULATION;
strcpy(city_large,CITY);
}
if(ELEVATION > high_elevation){
high_elevation = ELEVATION;
strcpy(city_high,CITY);
}
}
outfile<<"Largest: "<<city_large<<"City, "<<largest_population<<endl;
outfile<<"Highest: "<<city_high<<", "<<high_elevation<<endl;
infile.close();
outfile.close();
}
else{
return -1;
}
return 0;
}
readcity.txt :
___________
Seattle Wash. 668342 429
Denver Colo. 663862 5883
Washington DC 658893 16
Indianapolis Ind. 848788 797
NewYork N.Y. 8491079 13
LosAngeles Calif. 3928864 126
Sample Input and Output:
______________________
Enter input filename:readcity.txt
Enter output filename:output.txt
data stored in output file
output.txt :
_________
Largest: New York City, 8491079
Highest: Denver, 5883
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.