my program does not find the data

here is what I get when I run it:
-------------Election Results--------------

Candidate Votes
Name Region1 Region2 Region3 Region4 Total
----- ------- ------- ------- ------- -------
Donald 0 0 0 0 0
Ducky 0 0 0 0 0
Goldy 0 0 0 0 0
Goofy 0 0 0 0 0
Mickey 0 0 0 0 0
Monty 0 0 0 0 0


Winner: Donald, Votes Received: 0

Total votes polled: 0

Process returned 0 (0x0) execution time : 13.774 s
Press any key to continue.

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
//*************************************************************
// Author: D.S. Malik
//
// This program processes voting data for student council
// president's post. It outputs each candidate's name and the
// votes they received. The name of the winner is also printed.
//*************************************************************
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int NO_OF_CANDIDATES = 6;
const int NO_OF_REGIONS = 4;
void printHeading();
void initialize(int vbRegion[][NO_OF_REGIONS], int tVotes[],
int noOfRows);
void getCandidatesName(ifstream& inp, string cNames[],
int noOfRows);
void sortCandidatesName(string cNames[], int noOfRows);
int binSearch(string cNames[], int noOfRows, string name);
void processVotes(ifstream& inp, string cNames[],
int vbRegion[][NO_OF_REGIONS],
int noOfRows);
void addRegionsVote(int vbRegion[][NO_OF_REGIONS],
int tVotes[], int noOfRows);
void printResults(string cNames[],
int vbRegion[][NO_OF_REGIONS],
int tVotes[], int noOfRows);

int main()
{
//Declare variables; Step 1
string candidatesName[NO_OF_CANDIDATES];
int votesByRegion[NO_OF_CANDIDATES][NO_OF_REGIONS];
int totalVotes[NO_OF_CANDIDATES];
ifstream inp;
inp.open("candData.txt"); //Step 2
if (!inp) //Step 3
{
cout << "Input file (candData.txt) does "
<< "not exist." << endl;
return 1;
}
getCandidatesName(inp, candidatesName,
NO_OF_CANDIDATES); //Step 4
sortCandidatesName(candidatesName,
NO_OF_CANDIDATES); //Step 5
inp.close(); //Step 6
inp.clear(); //Step 6
inp.open("voteData.txt"); //Step 7
if (!inp) //Step 8
{
cout << "Input file (voteData.txt) does "
<< "not exist." << endl;
return 1;
}
initialize(votesByRegion, totalVotes,
NO_OF_CANDIDATES); //Step 9
void getCandidatesName(ifstream& inp, string cNames[],
int noOfRows);
void sortCandidatesName(string cNames[], int noOfRows);
int binSearch(string cNames[], int noOfRows, string name);
processVotes(inp, 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
return 0;
}
//Place the definitions of the functions initialize,
//getCandidatesName, sortCandidatesName, binSearch,
//processVotes, addRegionsVote, printHeading, and
//printResults here.
void initialize(int vbRegion[][NO_OF_REGIONS], 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, string cNames[],
int noOfRows)
{
int i;
for (i = 0; i < noOfRows; i++)
inp >> cNames[i];
}
void sortCandidatesName(string cNames[], int noOfRows)
{
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(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, 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],
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(string cNames[],
int vbRegion[][NO_OF_REGIONS],
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;
}

the array for the candidates names works fine.
in the other arrays there is something wrong.

please help
I don't know how to run your program because it requires me to open files.
Also, proper formatted was removed from your code in your attempt to copy it here, unlike your other posts, so maybe try to fix that for readability.

If you're expecting the values inside vbRegion to go up, then debug your program and watch to see what the value of noOfVotes is in processVotes.
Perhaps your while loop on line 140 is never being entered.

PS: Note on line 60, 61, and 62, you aren't actually calling a function. You just re-declared the function prototype.
Last edited on
The following code...

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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>


const int N_Of_Candidates = 6;
const int N_Of_Regions = 4;


void printHeading();

void initialize(int vbRegion[][N_Of_Regions], int tVotes[], int noOfRows);

void getCandidatesName(std::ifstream& inp, std::string cNames[], int noOfRows);

void sortCandidatesName(std::string cNames[], int noOfRows);

int binSearch(std::string cNames[], int noOfRows, std::string name);

void processVotes(std::ifstream& inp,
                  std::string cNames[],
                  int vbRegion[][N_Of_Regions],
                  int noOfRows);

void addRegionsVote(int vbRegion[][N_Of_Regions],
                    int tVotes[], int noOfRows);

void printResults(std::string cNames[],
                  int vbRegion[][N_Of_Regions],
                  int tVotes[],
                  int noOfRows);


int main()
{
    //Declare variables; Step 1
    std::string candidatesName[N_Of_Candidates];
    int votesByRegion[N_Of_Candidates][N_Of_Regions];
    int totalVotes[N_Of_Candidates];
    std::ifstream inp;

    inp.open("candData.txt"); //Step 2
    if (!inp) //Step 3
    {
        std::cout << "Input file (candData.txt) does not exist.\n";
        return 1;
    }

    getCandidatesName(inp,
                      candidatesName,
                      N_Of_Candidates); //Step 4

    sortCandidatesName(candidatesName,
                       N_Of_Candidates); //Step 5

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

    inp.open("voteData.txt"); //Step 7
    if (!inp) //Step 8
    {
        std::cout << "Input file (voteData.txt) does not exist.\n";
        return 1;
    }

    initialize(votesByRegion,
               totalVotes,
               N_Of_Candidates); //Step 9

    processVotes(inp,
                 candidatesName,
                 votesByRegion,
                 N_Of_Candidates); //Step 10

    addRegionsVote(votesByRegion,
                   totalVotes,
                   N_Of_Candidates); //Step 11

    printHeading(); //Step 12

    printResults(candidatesName,
                 votesByRegion,
                 totalVotes,
                 N_Of_Candidates); //Step 13
    return 0;
}


// Place the definitions of the functions initialize,
// getCandidatesName, sortCandidatesName, binSearch,
// processVotes, addRegionsVote, printHeading, and
// printResults here.
void initialize(int vbRegion[][N_Of_Regions], int tVotes[], int noOfRows)
{
    for (int i = 0; i < noOfRows; i++) {
        for (int j = 0; j < N_Of_Regions; j++) {
            vbRegion[i][j] = 0;
        }
    }
    for (int i = 0; i < noOfRows; i++) {
        tVotes[i] = 0;
    }
}


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


void sortCandidatesName(std::string cNames[], int noOfRows)
{
    //selection sort
    for (int i = 0; i < noOfRows - 1; ++i)
    {
        int min = i;
        for (int j = i + 1; j < noOfRows; ++j) {
            if (cNames[j] < cNames[min]) {
                min = j;
            }
        }
        cNames[i].swap(cNames[min]);
    }
}


int binSearch(std::string cNames[], int noOfRows, std::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(std::ifstream& inp,
                  std::string cNames[],
                  int vbRegion[][N_Of_Regions],
                  int noOfRows)
{
    std::string candName;
    int region;
    int noOfVotes;
    int loc;
    while (inp >> candName >> region >> noOfVotes)
    {
        loc = binSearch(cNames, noOfRows, candName);
        if (loc != -1) {
            vbRegion[loc][region - 1] += noOfVotes;
        }
        inp >> candName >> region >> noOfVotes;
    }
}


void addRegionsVote(int vbRegion[][N_Of_Regions],
                    int tVotes[],
                    int noOfRows)
{
    int i, j;
    for (i = 0; i < noOfRows; i ++) {
        for (j = 0; j < N_Of_Regions; j++) {
            tVotes[i] = tVotes[i] + vbRegion[i][j];
        }
    }
}


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


void printResults(std::string cNames[],
                  int vbRegion[][N_Of_Regions],
                  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];
        std::cout << std::left;
        std::cout << std::setw(9) << cNames[i] << " ";
        std::cout << std::right;
        for (j = 0; j < N_Of_Regions; j++) {
            std::cout << std::setw(8) << vbRegion[i][j] << " ";
        }
        std::cout << std::setw(6) << tVotes[i] << '\n';
    }
    std::cout << "\n\nWinner: " << cNames[winLoc]
              << ", Votes Received: " << tVotes[winLoc]
              << "\n\n";
    std::cout << "Total votes polled: " << sumVotes << '\n';
}


...with the following candData.txt...
1
2
3
4
5
6
Greg
Mickey
Lisa
Peter
Danny
Sheila


...and the following voteData.txt...
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
Greg    2   34
Mickey  1   56
Lisa    2   56
Peter   1   78
Danny   4   29
Sheila  4   78
Mickey  2   63
Lisa    1   23
Peter   2   56
Danny   1   25
Sheila  2   70
Peter   4   23
Danny   4   12
Greg    3  134
Sheila  4  100
Mickey  3   67
Lisa    2   67
Danny   3   67
Sheila  1   23
Mickey  1   56
Lisa    2   35
Sheila  3   78
Peter   1   27
Danny   2   34
Greg    1   75
Peter   4   23
Sheila  3   55
Mickey  4   67
Peter   1   23
Danny   3   89
Mickey  3   89
Peter   1   67
Danny   2   37
Sheila  4   89
Mickey  2   78
Lisa    1   87
Peter   1   90
Danny   4   56


...gives the following output:
 -------------Election Results--------------

Candidate Votes
Name        Region1   Region2    Region3  Region4   Total
-----       -------   -------    -------  -------  ------
Danny            0       37        0       41     78
Greg            75       34        0        0    109
Lisa             0      158        0        0    158
Mickey           0      141       89        0    230
Peter          140       56        0        0    196
Sheila          23       70       55      100    248


Winner: Sheila, Votes Received: 248

Total votes polled: 1019

Thank you all!!! good people. Both of these programs are now working fine.....

the old one which I posted, which was producing this output:
-------------Election Results--------------

Candidate Votes
Name Region1 Region2 Region3 Region4 Total
----- ------- ------- ------- ------- -------
Donald 0 0 0 0 0
Ducky 0 0 0 0 0
Goldy 0 0 0 0 0
Goofy 0 0 0 0 0
Mickey 0 0 0 0 0
Monty 0 0 0 0 0


Winner: Donald, Votes Received: 0

Total votes polled: 0

Process returned 0 (0x0) execution time : 13.774 s
Press any key to continue.
suddenly with the new data posted by Enoizat miraculously produces an output now:
-------------Election Results--------------

Candidate Votes
Name Region1 Region2 Region3 Region4 Total
----- ------- ------- ------- ------- -------
Danny 25 71 156 97 349
Greg 75 34 134 0 243
Lisa 110 158 0 0 268
Mickey 112 141 156 67 476
Peter 285 56 0 46 387
Sheila 23 70 133 267 493


Winner: Sheila, Votes Received: 493

Total votes polled: 2216

Process returned 0 (0x0) execution time : 0.043 s
Press any key to continue.

The only thing that have changed is the data.

Now this needs serious investigation as to why with the data in the textbook it produced zeros.
Now this needs serious investigation as to why with the data in the textbook it produced zeros.


1
2
3
4
5
6
7
void getCandidatesName(ifstream& inp, string cNames[],
                        int noOfRows)
{
    int i;
    for (i = 0; i < noOfRows; i++)
        inp >> cNames[i];
}


Could you please check if in your candData.txt file there is only one column of data
like so:
Greg
Mickey
Lisa
Peter
Danny
Sheila


and not 2 columns
like so:
Greg Goldy
Mickey Miller
Lisa Fisher
Peter Lamba
Danny Dillion
Sheila Bower


Your voteData.txt should be consistent with what above, i.e. it should be like this:
Greg 2 34
Mickey 1 56
Lisa 2 56
Peter 1 78
Danny 4 29
Sheila 4 78
...


and not like this:
Greg Goldy 2 34
Mickey Miller 1 56
Lisa Fisher 2 56
Peter Lamba 1 78
Danny Dillion 4 29
Sheila Bower 4 78
...



(Please note that getCandidatesName() cannot properly manage a name|space|surname structure.)

Thank you very much Enoizat!!! You are a life saver.
It all worked fine!!!!!
Topic archived. No new replies allowed.