Searching and Sorting an Array

Hi all,

I am having trouble with writing a function "void find_continents()"
that goes through all countries from a .csv file and fills the global array continents with all the unique names of continents. We can assume that there are no more than 20 unique continents but the function should not assume any other prior knowledge. You may declare other global variables if necessary.

To start I needed to open and read from the .csv file and then print the required info as shown.

This is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>     // exit

using namespace std;

const int N=247;   // number of countries 

struct Country {
    string name;   // name in English
    string continent;   
    unsigned int population;
    float area;    // sq. km
    float coastline;  // km
    string government;
    string currency;
    string currency_code;  // a 3-letter abbreviation
    float birth_rate;  // annual per 100 persons
    float death_rate;  // annual per 100 persons
    float life_expectancy;
};

Country countries[N];
string continents[20];


void load_countries(const char filename[]) {
    ifstream input;
    string line;
    char delimiter=';';
    size_t prev=0, pos; 

    input.open(filename);
    if(!input.is_open()) {
        cerr << "Cannot open file " << filename << endl;
        exit(0);
    }
    getline(input, line); // ignore the first line
    
    for(int i=0; i<N; i++) {
        getline(input, line);
        prev = 0;
        pos = line.find(delimiter, prev); 
        countries[i].name = line.substr(prev+1, pos-prev-2); 
        prev = pos+1;
        prev = line.find(delimiter,prev)+1; // skip Gername name
        prev = line.find(delimiter,prev)+1; // skip native name
        prev = line.find(delimiter,prev)+1; // skip country code
        pos = line.find(delimiter,prev);
        countries[i].continent = line.substr(prev+1, pos-prev-2);
        prev = pos+1;
        prev = line.find(delimiter,prev)+1;  // skip capital
        pos = line.find(delimiter,prev);
        countries[i].population = stoi(line.substr(prev, pos-prev));
        prev = pos+1;
        pos = line.find(delimiter,prev);
        countries[i].area = stof(line.substr(prev, pos-prev));
        prev = pos+1;
        pos = line.find(delimiter,prev);
        countries[i].coastline = stof(line.substr(prev, pos-prev));
        prev = pos+1;
        pos = line.find(delimiter,prev);
        countries[i].government = line.substr(prev+1, pos-prev-2); 
        prev = pos+1;
        pos = line.find(delimiter,prev);
        countries[i].currency = line.substr(prev+1, pos-prev-2);
        prev = pos+1;
        pos = line.find(delimiter,prev);
        countries[i].currency_code = line.substr(prev+1, pos-prev-2); 
        prev = pos+1;
        prev = line.find(delimiter,prev)+1; // skip dialing prefix
        pos = line.find(delimiter,prev);
        countries[i].birth_rate = stof(line.substr(prev, pos-prev));
        prev = pos+1;
        pos = line.find(delimiter,prev);
        countries[i].death_rate = stof(line.substr(prev, pos-prev));
        prev = pos+1;
        pos = line.find(delimiter,prev);
        countries[i].life_expectancy = stof(line.substr(prev, pos-prev));
    }
    input.close();
}

void print_country(int i) {
    cout << "COUNTRY: " << countries[i].name << " (" << countries[i].continent << ") " << endl;
    cout << "  Population: " << countries[i].population << endl;
    cout << "  Area:       " << countries[i].area << " (km^2)" << endl;
    cout << "  Population density: " << countries[i].population/countries[i].area << " (persons per km^2)" << endl;
    cout << "  Coastline:  " << countries[i].coastline << " (km)" << endl;
    cout << "  Currency:   " << countries[i].currency << " (" << countries[i].currency_code << ")" << endl;
    cout << endl;
}
    
    

int main() {
    load_countries("countries.csv");
    for(int i=0; i<N; i++)
        print_country(i);
    return 0;
}


After I write this function I must make another one that takes the name of the continent and prints its total population, total area, total coastline length, and the number of countries for each of the continents found by find_continents. I will call this function void print_continent_stats(const string& continent).

Any help is appreciated, thanks.
Last edited on
Topic archived. No new replies allowed.