Linked List Dynamic Array?

I'm trying to make a dynamic array, I have a linked list of structs getting data from a csv file, but the values don't seem to be saved in it, so I continuously get outputs of zero regardless of the numbers I input to check the data, or the code just doesn't run. I'm also not to sure about how to create a dynamic array with linked lists, but I can't use anything else (no vectors allowed, can't use classes). Help?

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include<fstream>
using namespace std;




struct CountryData {
    double pop1950;
    double pop1970;
    double pop1990;
    double pop2010;
    double pop2015;
    string name;
};
struct Node {
    CountryData country;
    Node *      next;
};



//function
int lineCounter(Node *temp1, int s, int t);


int main(int argc, const char * argv[]) {
    
    
    ifstream file;
    
    if(file.fail())
    {
        cout<<"unable to open the file"<<endl;
        exit(1);
    }
    file.open("population.csv");
    //making linked list
    
    Node *N; 
    Node *world = NULL;
    double a, b, c, d, e;
    string line;
    Node nodes;
    
    
    
    while(file>>a>>b>>c>>d>>e && (getline(file,line))) {
        nodes.country.pop1950 = a;
        nodes.country.pop1970 = b;
        nodes.country.pop1990 = c;
        nodes.country.pop2010 = d;
        nodes.country.pop2015 = e;
        nodes.country.name = line;
        
        
        cout<<a<<b<<c<<d<<e<<line<<endl;
        
        
        
        if(world == NULL) {
            
            
            N = new Node;
            N->country.pop1950 = a;
            N->country.pop1970 = b;
            N->country.pop1990 = c;
            N->country.pop2010 = d;
            N->country.pop2015 = e;
            N->country.name = line;
            N->next = world;
            world = N;
            
        }
        else {
            N = new Node;
            N->country.pop1950 = a;
            N->country.pop1970 = b;
            N->country.pop1990 = c;
            N->country.pop2010 = d;
            N->country.pop2015 = e;
            N->country.name = line;
            N->next = world;
            world = N;
            cout<<a<<b<<c<<d<<e<<line<<endl;
            
        }
    }
    //range check for population
    double m, n;
    do
    {
        cout << "enter minimum (inclusive) for population range" << endl;
        cin >> m;
    }while(m < 0);
    do
    {
        cout << "enter maximum (inclusive) for population range (has to be larger than minimum)." << endl;
        cin >> n;
    }
    
    while(m >= n || n < 0);
    
    
    
    
    Node *o = NULL;

//function call
    int counter = lineCounter(world, m, n);
    
    o = new Node[counter];
    
    
  
    
    
    
    if(e >= m && e <= n)
    {
      
        cout<<"hello"<<endl;
            cout<<"Hi"<<endl;
            
    }
    cout<<counter<<endl;
    cout << "There are " << counter << "countries that have populations between " << m << " and " << n << "." << endl;
    for(int i = 0; i < counter; i++) {
        cout << o->country.name << ": " << o->country.pop2015 << endl;
    }
    
    delete[] o;
}

//function to pass list by reference(?)

int lineCounter(Node * temp1, int s, int t) {
    {
    string line;
    double a;
    double b;
    double c;
    double d;
    double e;
    //int j
    int count;
    //int *arr[j];
    ifstream file("population.csv");
    while(!file.eof())
    {
        file >> a >> b >> c >> d >> e;
        getline (file, line);
        
        
        temp1 = new Node;
        if(temp1 == NULL)
        {
            cout<<endl<<"The linked list is empty"<<endl;
        }
        else
        {
            cout<<"Linked list: ";
            while( temp1!=NULL )
            {
                cout<<temp1->country.pop1950<<" ";	// show the data in the linked list
                cout<<temp1->country.pop1970<<" ";
                cout<<temp1->country.pop1990<<" ";
                cout<<temp1->country.pop2010<<" ";
                cout<<temp1->country.pop2015<<" ";
                cout<<temp1->country.name<<" ";
                temp1 = temp1->next;
            }
        }
       
        file >> a >> b >> c >> d >> e;
        getline (file, line);
        if((e >= s && e <= t)) {
            count++;
        }
        }
    return count;
    }
        }
Last edited on
@Shikana
You are lucky. I have the same assignment and luckily I have finished it.

Can you help me?
Last edited on
Topic archived. No new replies allowed.