vectors

Ok, so I am trying to run this code but it's not giving me any errors. So I have no idea what's wrong with it. It couts the results in a table but the vectors are empty somehow.
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
 
using namespace std;

const int NO_OF_CANDIDATES = 6;
const int NO_OF_REGIONS = 4;

void printHeading();

void initialize(int vbRegion[][NO_OF_REGIONS], vector<int> tVotes,
                int noOfRows);

void getCandidatesName(ifstream& inp, vector<string> cNames, 
                       int noOfRows);

void sortCandidatesName(vector<string> cNames, int noOfRows);

int binSearch(vector<string> cNames, int noOfRows, string name);

void processVotes(ifstream& inp, vector<string> cNames,
                  int vbRegion[][NO_OF_REGIONS], 
                  int noOfRows);

void addRegionsVote(int vbRegion[][NO_OF_REGIONS], 
                    vector<int> tVotes, int noOfRows);

void printResults(const vector<string> cNames, 
                  int vbRegion[][NO_OF_REGIONS],
                  vector<int> tVotes, int noOfRows);

int main()
{
        //Declare variables; Step 1
    vector<string> candidatesName(NO_OF_CANDIDATES);
    int votesByRegion[NO_OF_CANDIDATES][NO_OF_REGIONS];
    vector<int> totalVotes(NO_OF_CANDIDATES);
    ifstream infile;

    infile.open("candData.txt");                    //Step 2
    if (!infile)                                    //Step 3
    {
        cout << "Input file (candData.txt) does " 
             << "not exit." << endl;
        return 1;
    }

    getCandidatesName(infile, candidatesName, 
                      NO_OF_CANDIDATES);            //Step 4
    //sortCandidatesName(candidatesName, 
    //                   NO_OF_CANDIDATES);           //Step 5

    infile.close();                                 //Step 6
    infile.clear();                                 //Step 6

    infile.open("voteData.txt");                    //Step 7
    if (!infile)                                    //Step 8
    {
        cout << "Input file (voteData.txt) does "
             << "not exist." << endl;
        return 1;
    }

    //initialize(votesByRegion, totalVotes, 
     //          NO_OF_CANDIDATES);                   //Step 9
    //processVotes(infile, candidatesName, 
     //            votesByRegion, NO_OF_CANDIDATES);  //Step 10
   // addRegionsVote(votesByRegion, totalVotes,
   //                NO_OF_CANDIDATES);               //Step 11

    printHeading();                                 //Step 12
    printResults(candidatesName,votesByRegion,
                 totalVotes, NO_OF_CANDIDATES);     //Step 13
	system("pause");
    return 0;
}

void initialize(int vbRegion[][NO_OF_REGIONS], vector<int> tVotes,
                int noOfRows)
{
    int i, j;

    for (i = 0; i < noOfRows; i++)
        for (j = 0; j < NO_OF_REGIONS; j++)
            vbRegion[i][j] = 0;

    for (i = 0; i < noOfRows; i++)
        tVotes[i] = 0;
}

void getCandidatesName(ifstream& inp, vector<string> cNames, 
                       int noOfRows)
{
    int i;
	
    for (i = 0; i < noOfRows; i++)
        inp >> cNames[i];
}

void sortCandidatesName(vector<string> cNames, int noOfRows)
{
    int firstOutOfOrder, location;
    string temp;

    for (firstOutOfOrder = 1; firstOutOfOrder < noOfRows;
                              firstOutOfOrder++)
        if (cNames[firstOutOfOrder] < cNames[firstOutOfOrder - 1])
        {
            temp = cNames[firstOutOfOrder];
            location = firstOutOfOrder;

            do
            {
                cNames[location] = cNames[location - 1];
                location--;
            } 
            while (location > 0 && cNames[location - 1] > temp);

            cNames[location] = temp;
        }

/*
    int i, j;
    int min;

        //selection sort
    for (i = 0; i < noOfRows - 1; i++)
    {
        min = i;

        for (j = i + 1; j < noOfRows; j++)
            if (cNames[j] < cNames[min])
                min = j;

        cNames[i].swap(cNames[min]);
    }
    */
}

int binSearch(vector<string> cNames, int noOfRows, string name)
{
    int first, last, mid;
    bool found;
    first = 0;
    last = noOfRows - 1;
    found = false;

    while (!found && first <= last)
    {
        mid = (first + last) / 2;

        if (cNames[mid] == name)
            found = true;
        else if (cNames[mid] > name)
            last = mid - 1;
        else
            first = mid + 1;
    }

    if (found)
        return mid;
    else
        return -1;
}

void processVotes(ifstream& inp, vector<string> cNames,
                  int vbRegion[][NO_OF_REGIONS], 
                  int noOfRows)
{
    string candName;
    int region;
    int noOfVotes;
    int loc;

    inp >> candName >> region >> noOfVotes;

    while (inp)
    {
        loc =  binSearch(cNames, noOfRows, candName);

        if (loc != -1)
            vbRegion[loc][region - 1] = vbRegion[loc][region - 1]
                                        + noOfVotes;
        inp >> candName >> region >> noOfVotes;
    }
}

void addRegionsVote(int vbRegion[][NO_OF_REGIONS], 
                    vector<int> tVotes, int noOfRows)
{
    int i, j;

    for (i = 0; i < noOfRows; i ++)
        for (j = 0; j < NO_OF_REGIONS; j++)
            tVotes[i] = tVotes[i] + vbRegion[i][j];
}

void printHeading()
{
    cout << "      --------------Election Results----------" 
         << "----" << endl << endl;
    cout << "Candidate                  Votes" << endl;
    cout << "Name        Region1   Region2   Region3   "
         << "Region4    Total" << endl;
    cout << "---------   -------   -------   -------   "
         << "-------   ------" << endl;
}

void printResults(vector<string> cNames, 
                  int vbRegion[][NO_OF_REGIONS],
                  vector<int> tVotes, int noOfRows)
{
    int i, j;
    int largestVotes = 0;
    int winLoc = 0;
    int sumVotes = 0;

    for (i = 0; i < noOfRows; i++)
    {
        if (largestVotes < tVotes[i])
        {
            largestVotes = tVotes[i];
            winLoc = i;
        }

        sumVotes = sumVotes + tVotes[i];

        cout << left;
        cout << setw(9) << cNames[i] << "  ";
        cout << right;
        for (j = 0; j < NO_OF_REGIONS; j++)
            cout << setw(8) << vbRegion[i][j] << "  ";
        cout << setw(6) << tVotes[i] << endl;
    }

    cout << endl << endl << "Winner: " << cNames[winLoc]
         << ", Votes Received: " << tVotes[winLoc] 
         << endl << endl;
    cout << "Total votes polled: " << sumVotes << endl;
}
You need to pass your vectors by reference if you want them to keep the data in the original vector. Otherwise is it just creating a copy when passed then forgetting it all.

I suggest you look up "Passing by reference" vs "Passing by value" for more info.
I saw people doing vector& but I get an error. Should it be for example candidateName&. I've looked it up but I'm still confused.
1
2
 void getCandidatesName(ifstream& inp, vector<string> &cNames, 
                       int noOfRows) 

Don't forget to change it in the function definition at the top as well!
Topic archived. No new replies allowed.