help with declarations

I posted something recently for one specific problem, but I realized the problem is general, and I need help with the concept. I have done research, but I just can't remember.

string ID[uniqueVotes] = {};
How would I declare uniqueVotes? I get an error that it is not declared in the scope.

int size = sizeof(currentID) / sizeof(string);
Same with this one.

Thanks for any help!
string ID[uniqueVotes] = {};
How would I declare uniqueVotes? I get an error that it is not declared in the scope.

That is both a declaration and a definition.


int size = sizeof(currentID) / sizeof(string);
Same with this one.

Also both a declaration and definition.

Context is required.
This is the whole function

void remove_duplicate()//this function removes the duplicate IDs

{

ifstream myfile("counted.txt");
string ID[uniqueVotes] = {};
int size = sizeof(currentID) / sizeof(string);

string temp = "";
for (int i = 0; i < size; i++)
{
temp = currentID[i];
for (int j = i + 1; j < size; j++)
{
if (temp == currentID[j])
currentID[j] = "";
}
}

for (int i = 0; i < size; i++)
{
cout << currentID[i] << endl;
}
}
void remove_duplicate()//this function removes the duplicate IDs

{

ifstream myfile("counted.txt");
string ID[uniqueVotes] = {};
int size = sizeof(currentID) / sizeof(string);

string temp = "";
for (int i = 0; i < size; i++)
{
temp = currentID[i];
for (int j = i + 1; j < size; j++)
{
if (temp == currentID[j])
currentID[j] = "";
}
}

for (int i = 0; i < size; i++)
{
cout << currentID[i] << endl;
}
}
That is the whole function.
Maybe you should provide the exact error that is generated. It doesn't actually have to do with uniqueVotes does it? And what is currentID?
Last edited on
"'uniqueVotes' was not declared in this scope." Same for currentID. This function is supposed to remove duplicate votes.
Well, uniqueVotes was not declared in that scope. Neither was currentID, although it looks like the refereneces to currentID should be to ID which is declared in that scope.

Is this function supposed to be a member of a class? Is uniqueVotes a global variable? A member of the class this function belongs to? (It must be a compile-time constant to be used in the context of an array size.)
The error is still showing. I've been looking at these 2 errors all day lol. Just can't fix them.
The error is still showing.


The answers you provided to the questions I asked are very enlightening.
Sorry, didn't even see that post lol.
It is supposed to be a member of a class that this function belongs to.
They are global variables.
Is the function definition you showed here inline in the class or in a separate implementation file?
This is the entire project. I figured it easier that way.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>

using namespace std;
// TESTING FILE STREAM OPERATOR


int main() //this function inputs the data and creates three parallel arrays
{
std::ifstream in("ballots.txt");
std::stringstream buffer;
buffer << in.rdbuf();
std::string input = buffer.str();
std::cout << input << std::endl << std::endl;

string votes[uniqueVotes];
int currentID;
int YN; //yes or no
string ID[uniqueVotes]; //the first vote a member casts
int ID; //Hogwart ID number
char ADM1, ADM2, ADM3, ADM4, ADM5, ADM6, ADM7, ADM8, ADM9, ADM10; //vote on amendments
string vote; //vote for captain

ifstream fileReader; //open file for reading
fileReader.open("ballots.txt");
if (fileReader.fail())
{
cout << "The ballots failed to open with code: " << fileReader.failbit;
}
else
{
do
{
fileReader >> ID;
cout << "Id:" << ID;
fileReader >> ADM1;
fileReader >> ADM2;
fileReader >> ADM3;
fileReader >> ADM4;
fileReader >> ADM5;
fileReader >> ADM6;
fileReader >> ADM7;
fileReader >> ADM8;
fileReader >> ADM9;
fileReader >> ADM10;
cout << " Amendments 1 - 10: " << ADM1 << ADM2 << ADM3 << ADM4 << ADM5 << ADM6 << ADM7 << ADM8 << ADM9 << ADM10;
getline(fileReader,vote);
//fileReader >> vote;
cout << " the votee: " << vote << endl;


//fileReader.ignore(30,'\n');
} while(!fileReader.eof());

}

fileReader.close();

if (ID[uniqueVotes] != currentID)

{

//if this is a new ID, fill next row of parallel arrays

for (int j = 0; j < 10; j++){

fileReader >> YN[uniqueVotes][j];

}

getline(fileReader,vote[uniqueVotes]);



currentID = ID[uniqueVotes]; //set new current ID



uniqueVotes++; //new uniqueVote ID was found

}



return 0;
}


void remove_duplicate()//this function removes the duplicate IDs

{

std::string ID (uniqueVotes, " ");
int uniqueVotes;

ifstream myfile("counted.txt");
string ID[uniqueVotes] = {};
int size = sizeof(currentID) / sizeof(string);

string temp = "";
for (int i = 0; i < size; i++)
{
temp = currentID[i];
for (int j = i + 1; j < size; j++)
{
if (temp == currentID[j])
currentID[j] = "";
}
}

for (int i = 0; i < size; i++)
{
cout << currentID[i] << endl;
}
}

int winner () //This function outputs the election winner
{
cout << "55 staff members voted, using 99 ballots" << endl;
cout << "The Headmaster goes to 'New Guy', with 31 votes." << endl;
cout << "The runner-up is Harry Potter, with 24 votes." << endl;

}

int amendment_tally () //this function outputs amendment winners
{
cout << "Amendment 1: 29 yes votes, 26 no votes. Amendment 1 fails to reach a 2/3 majority." << endl;
cout << "Amendment 2: 38 yes votes, 17 no votes. Amendment 2 succeeds in obtaining a 2/3 majority." << endl;
cout << "Amendment 3: 30 yes votes, 25 no votes. Amendment 3 fails to reach a 2/3 majority." << endl;
cout << "Amendment 4: 37 yes votes, 18 no votes. Amendment 4 succeeds in obtaining a 2/3 majority." << endl;
cout << "Amendment 5: 35 yes votes, 20 no votes. Amendment 5 fails to reach a 2/3 majority." << endl;
cout << "Amendment 6: 21 yes votes, 34 no votes. Amendment 6 fails to reach a 2/3 majority." << endl;
cout << "Amendment 7: 48 yes votes, 7 no votes. Amendment 7 succeeds in obtaining a 2/3 majority." << endl;
cout << "Amendment 8: 16 yes votes, 39 no votes. Amendment 8 fails to reach a 2/3 majority." << endl;
cout << "Amendment 9: 48 yes votes, 7 no votes. Amendment 9 succeeds in obtaining a 2/3 majority." << endl;
cout << "Amendment 10: 20 yes votes, 35 no votes. Amendment 10 fails to reach a 2/3 majority." << endl;
cout << "In total, 4 amendments passed, with 6 failing to pass" << endl;
}
Come on, 19 posts now code tags:

http://www.cplusplus.com/articles/z13hAqkS/
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

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>

using namespace std;
// TESTING FILE STREAM OPERATOR


int main() //this function inputs the data and creates three parallel arrays
{
std::ifstream in("ballots.txt");
std::stringstream buffer;
buffer << in.rdbuf();
std::string input = buffer.str();
std::cout << input << std::endl << std::endl;

string votes[uniqueVotes];
int currentID;
int YN; //yes or no
string ID[uniqueVotes]; //the first vote a member casts
int ID; //Hogwart ID number
char ADM1, ADM2, ADM3, ADM4, ADM5, ADM6, ADM7, ADM8, ADM9, ADM10; //vote on amendments
string vote; //vote for captain

ifstream fileReader; //open file for reading
fileReader.open("ballots.txt");
if (fileReader.fail())
{
cout << "The ballots failed to open with code: " << fileReader.failbit;
}
else
{
do
{
fileReader >> ID;
cout << "Id:" << ID;
fileReader >> ADM1;
fileReader >> ADM2;
fileReader >> ADM3;
fileReader >> ADM4;
fileReader >> ADM5;
fileReader >> ADM6;
fileReader >> ADM7;
fileReader >> ADM8;
fileReader >> ADM9;
fileReader >> ADM10;
cout << " Amendments 1 - 10: " << ADM1 << ADM2 << ADM3 << ADM4 << ADM5 << ADM6 << ADM7 << ADM8 << ADM9 << ADM10;
getline(fileReader,vote);
//fileReader >> vote;
cout << " the votee: " << vote << endl;


//fileReader.ignore(30,'\n');
} while(!fileReader.eof());

}

fileReader.close();

if (ID[uniqueVotes] != currentID)

{

//if this is a new ID, fill next row of parallel arrays

for (int j = 0; j < 10; j++){

fileReader >> YN[uniqueVotes][j];

}

getline(fileReader,vote[uniqueVotes]);



currentID = ID[uniqueVotes]; //set new current ID



uniqueVotes++; //new uniqueVote ID was found

}



return 0;
}


void remove_duplicate()//this function removes the duplicate IDs

{

std::string ID (uniqueVotes, " ");
int uniqueVotes;

ifstream myfile("counted.txt");
string ID[uniqueVotes] = {};
int size = sizeof(currentID) / sizeof(string);

string temp = "";
for (int i = 0; i < size; i++)
{
temp = currentID[i];
for (int j = i + 1; j < size; j++)
{
if (temp == currentID[j])
currentID[j] = "";
}
}

for (int i = 0; i < size; i++)
{
cout << currentID[i] << endl;
}
}

int winner () //This function outputs the election winner
{
cout << "55 staff members voted, using 99 ballots" << endl;
cout << "The Headmaster goes to 'New Guy', with 31 votes." << endl;
cout << "The runner-up is Harry Potter, with 24 votes." << endl;

}

int amendment_tally () //this function outputs amendment winners
{
cout << "Amendment 1: 29 yes votes, 26 no votes. Amendment 1 fails to reach a 2/3 majority." << endl;
cout << "Amendment 2: 38 yes votes, 17 no votes. Amendment 2 succeeds in obtaining a 2/3 majority." << endl;
cout << "Amendment 3: 30 yes votes, 25 no votes. Amendment 3 fails to reach a 2/3 majority." << endl;
cout << "Amendment 4: 37 yes votes, 18 no votes. Amendment 4 succeeds in obtaining a 2/3 majority." << endl;
cout << "Amendment 5: 35 yes votes, 20 no votes. Amendment 5 fails to reach a 2/3 majority." << endl;
cout << "Amendment 6: 21 yes votes, 34 no votes. Amendment 6 fails to reach a 2/3 majority." << endl;
cout << "Amendment 7: 48 yes votes, 7 no votes. Amendment 7 succeeds in obtaining a 2/3 majority." << endl;
cout << "Amendment 8: 16 yes votes, 39 no votes. Amendment 8 fails to reach a 2/3 majority." << endl;
cout << "Amendment 9: 48 yes votes, 7 no votes. Amendment 9 succeeds in obtaining a 2/3 majority." << endl;
cout << "Amendment 10: 20 yes votes, 35 no votes. Amendment 10 fails to reach a 2/3 majority." << endl;
cout << "In total, 4 amendments passed, with 6 failing to pass" << endl;
}
Never knew what that meant til now lol.
They are global variables.

No, they are not.

Aside from what looks like a shoddy attempt at fixing the problem with the definition of uniqueVotes as an uninitialized variable of type int which is local to remove_duplicate (after its use was attempted,) there is no definition or declaration anywhere in your code.

Do you know what a global variable is?
closed account (48T7M4Gy)
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
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::endl;
using std::string;
using std::ifstream;

void remove_duplicates();
int winner();
int amendment_tally();
void remove_duplicates(int[], int);

int main()
{
	const int noVotes = 100;
	string votes[noVotes];
	int voterNo = 0;

	ifstream in("ballots.txt");

	int ID[noVotes];
	char ADM[noVotes][10];

	ifstream fileReader;
	fileReader.open("ballots.txt");

	if (fileReader.fail())
		cout << "Failed to open" << endl;
	else
	{
		voterNo = 0;
		while (!fileReader.eof())
		{
			fileReader >> ID[voterNo];
			for (int i = 0; i < 10; i++)
				fileReader >> ADM[voterNo][i];

			cout << " Amendments 1 - 10: " << endl;
			for (int i = 0; i < 10; i++)
				cout << ADM[voterNo][i] << ' ';
			cout << endl;

			voterNo++;
		}
	}
	fileReader.close();

	remove_duplicates( ID, voterNo );

	return 0;
}


void remove_duplicates(int anIDArray[], int size)
{
	int temp = 0;
	for (int i = 0; i < size; i++)
	{
		temp = anIDArray[i];
		for (int j = i + 1; j < size; j++)
		{
			if (temp == anIDArray[j])
				anIDArray[j] = 0;
		}
	}

	for (int i = 0; i < size; i++)
		cout << anIDArray[i] << endl;
}

int winner()
{
	cout << "STUB: winner()" << endl;
	return 0;
}

int amendment_tally()
{
	cout << "STUB: amendment_tally()" << endl;
	return 0;
}//E&OE 


This is not complete and I have no data to check it including possible pass-by-reference issues but I think it is clearer on how the problem of removing duplicates can be handled.

Note the two stubs, both return ints but all they do is print stuff.

PS no_of_voters would be a better name than voterNo
Last edited on
Topic archived. No new replies allowed.