Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the programming language C++ The file name is WorldPopAndArea.txt Please use

ID: 3730954 • Letter: U

Question

Use the programming language C++

The file name is WorldPopAndArea.txt

Please use ifstreams in this program

III. Working with Files Start up a new project and add a source file (.cpp file). Your source code must include the following header comment at the top of the file: //Lab Assignment #8, Working with Files //Program Name: //Purpose of the program: //Authors: Your and your partner' s names //Date: write today' s date Download the file WorldPopAndArea.txt. Save the file in the same folder as your source code file. We will use the contents of this file as data for our program. This file contains the following information for each country in the World: name of the country, population of the country, and the area of the country (given in Km2). Here are the first few lines of this file: 647500 Afghanistan 32738376 Akrotiri Albania Algeria American Samoa 57496 199 Andorra Angola 12531357 Anguilla Antigua and Barbuda 15700 123 3619778 28748 33769669 2381740 72413 468 1246700 14108 102 69842 443 You and your partner will write a program that uses the data in WorldPopAndArea.txt to calculate the population density for each country (that is, the number of people per square kilometer). The results should be stored in an output file that contains: the names of the countries and their population densities Your program should Ask the user to enter the names of the input file and the output file Check that the input file can be opened. If not, then let the user know that there was a problem opening the file Call a function that receives the input and output file streams. This function will read all the data until the end of file, perform the density calculation for each country and save the results in an ouput file. This is a void function. *

Explanation / Answer

Note: Make sure your input file and program is saved in the same directory

Feel free to ask doubts in the comment section :)

CODE

//Lab Assignment #8, Working with Files
//Program Name: PopDensity.cpp
//Authors:
//Date:
#include<bits/stdc++.h>
using namespace std;
void getPopDensity(string inputfile, string outputfile)
{
string country;
double population, area,popDensity;
ifstream infile(inputfile.c_str());
ofstream outfile(outputfile.c_str()); //output will be printed to entered output file
while(infile)
{
infile>>country>>population>>area; //takes input from the file in the declared variables
popDensity = population/area;
outfile<<setw(50)<<left<<country<<fixed<<setprecision(2)<<setw(25)<<right<<popDensity<<endl;
//setw(x) : set the width of output to 50 char
// left : justify the output to left
// right : justify the output to right
// setprecision(x) : print x digits after decimal
// all above are called MANIPULATOR
}
infile.close(); //closing the input file
outfile.close(); //closing the output file
}
int main()
{
string inputfile, outputfile;
cout<<"Enter the name of input file ";
cin>>inputfile;
cout<<"Enter the name of output file ";
cin>>outputfile;

ifstream infile(inputfile.c_str()); //This will open given file to accept input
if(infile == NULL)
cout<<"There was a problem opening file. Please Try again later!";
else
{
infile.close();
getPopDensity(inputfile,outputfile);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote