Need help debugging my declared vectors in functions

closed account (LNAM4iN6)
H
Last edited on
int summation(const vector<int> total){
You're trying to add items to the end of a const vector (const is the qualifier it is complaining about you trying to discard)

total.at[0] at is a function. Use brackets to pass parameters to functions (i.e. total.at(0)
Last edited on
OP for reference:

Hi all,
I've seem to come to a roadblock in my computer code. Up to this point, I have everything running smoothly, except I keep getting the error "invalid types â<unresolved overloaded function type>[int]â for array subscript" three times for line 127 of my code. I am also getting this strange "error: passing âconst std::vector<int, std::allocator<int> >â as âthisâ argument of âvoid std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]â discards qualifiers" error on lines 123, 124, and 125. I've scoured all over the forums but anything relating to these problems doesn't make sense to me. Any sort of help would be greatly appreciated! I am trying to create an int function to find the summation of the integers in three different vectors.


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
#include<iostream>
#include<iomanip>
#include<fstream>
#include<vector>
#include<string>
using namespace std;

void addfile(const string, vector<string> &, vector<int> &, vector<int> &, vector<int> &);
void printresults(const vector<string>, const vector<int>, const vector<int>, const vector<int>);
int summation(const vector<int>);

int main()
{
char choice;
string filename = " ";
vector<string> partyname;
vector<int> nullifiervotes;
vector<int> fielditevotes;
vector<int> othervotes;
vector<int> totalvotes;

do {
cout << "Add a county election file             A" << endl;
cout << "Show election totals on screen         P" << endl;
cout << "Search for county results              S" << endl;
cout << "Exit the program                       Q" << endl << endl;
cout << "Please enter your choice: ";
cin >> choice;

switch (choice){
        case 'A':
        case 'a':
        addfile(filename, partyname, nullifiervotes, fielditevotes, othervotes);
        break;
        case 'P':
        case 'p':
        printresults(partyname, nullifiervotes, fielditevotes, othervotes);
        summation(totalvotes);
        case 'S':
        case 's':
        cout << "You chose " << choice << endl;
        break;
        case 'Q':
        case 'q':
        cout << "Ending program..." << endl;
        break;
        default:
        cout << "Not a valid choice" << endl;}
}
while   ((choice == 'A')||(choice == 'a')||(choice == 'P')||(choice == 'p')
        ||(choice == 'S')||(choice == 's'));

return 0;
}

void addfile(const string file, vector<string> &party, vector<int> &nullifier, vector<int> &fieldite, vector<int> &other)
{
ifstream textfile;
int totalnullifiervotes = 0;
int totalfielditevotes = 0;
int totalothervotes = 0;
string name;
char possibilities;
string filename;

cout << "Enter the county file to process: ";
cin >> filename;
textfile.open(filename.c_str());

if (!textfile){
        cout << "File not found. Please enter a valid file." << endl;
        }

getline(textfile, name);
party.push_back(name);

while (textfile >> possibilities) {
        switch(possibilities) {
                case 'N':
                case 'n':
                totalnullifiervotes++;
                break;
                case 'F':
                case 'f':
                totalfielditevotes++;
                break;
                default:
                totalothervotes++;
                break;
}}
nullifier.push_back(totalnullifiervotes);
fieldite.push_back(totalfielditevotes);
other.push_back(totalothervotes);
return;
}

void printresults(const vector<string> party, const vector<int> nullifier, const vector<int> fieldite, const vector<int> other)
{
cout << "County" << setw(15) << "Nullifier" << setw(15) << "Fieldite" << setw(15) << "Third Party" << endl;
cout << "------------------------------------------------------------------" << endl;
for (int i=0, j=0, k=0, l=0; i < party.size(), j < nullifier.size(), k < fieldite.size(), l < other.size(); i++, j++, k++, l++){
        cout << party.at(i) << setw (15) << nullifier.at(j) << setw (15) << fieldite.at(k) << setw (15) << other.at(l) << endl;
        }


cout << "------------------------------------------------------------------" << endl;
return;
}

int summation(const vector<int> total){

const vector<int> nullifier;
const vector<int> fieldite;
const vector<int> other;
int nullifiersum = 0;
int fielditesum = 0;
int othersum = 0;

for (int i = 0; i < nullifier.size(); i++){
        nullifiersum += nullifier[i];}
for (int i = 0; i < fieldite.size(); i++){
        fielditesum += fieldite[i];}
for (int i = 0; i < other.size(); i++){
        othersum += other[i];}

total.push_back(nullifiersum);
total.push_back(fielditesum);
total.push_back(othersum);

cout << setw(20) << total.at[0] << setw (10) << total.at[1] << setw (10) << total.at[2] << endl;
return 0;
}
Topic archived. No new replies allowed.