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

This is the file name: populations.csv The first 5 numbers are populations in 19

ID: 3761915 • Letter: T

Question

This is the file name: populations.csv

The first 5 numbers are populations in 1950, 1970, 1990, 2010, and 2015 respectively. Then the country name.

I have to use a structure:

  struct Country {

            string name;

            double pop1950;

            double pop1970;

            double pop1990;

            double pop2010;

            double pop2015;

};

This is what I have so far:

  void myWorld(){

ifstream data;

data.open("population_corrected.csv")

while (population_corrected.csv >> name) {

getline(population_corrected.csv, name);

}

}

Write a function to initialize m)Wodd to data read from the supplied data file. There are a few complications Reading the populations is straightforward, and can be done using the standard file IO functions.i.e., ">>" using an input stream). However, since the names of some countries contain spaces, we need to use getline instead of>>for the name field. Fortunately, the country is the final field so we can use ">>" to read the populations and then a getline to finish the line. You will need to input data until the end of file is reached. Recall that getline's return value is false if at end of file, so its easy to check for this » » To check that you did this correctly, you may wish to use ddd to check the value of myWorld

Explanation / Answer

int countOfCountries=0;

struct Country {

string name;

double pop1950;

double pop1970;

double pop1990;

double pop2010;

double pop2015;

} countries[50];

void myWorld(){

ifstream data;

data.open("population_corrected.csv");

  

while (!data.eof()) {

cin>>countries[countOfCountries].pop1950>>countries[countOfCountries].pop1970>>countries[countOfCountries].pop1970>>countries[countOfCountries].pop2010>>countries[countOfCountries].pop2015;

getline(data, countries[countOfCountries].name);

countOfCountries++;

}

}

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