Bool funtion error need return value very stumped, help!

I am getting an error C2131: expression did not evaluate to a constant , failure was caused by a read of a variable outside its lifetime, and see usage of 'n'

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

using namespace std;

bool readFile(string[], int *);
void calculatePercents(int v[], double p[], int);
void displayResults(string c[], int v[], double p[], int);
void getVotes(int v[], int);

int main()
{
	int n = 25;
	string c[n];
	int v[n];
	double p[n];
	getVotes(v, n);
	readFile(c, &n); //calling function to read candiadtes names from files
	calculatePercents(v, p, n);//to get votes using random number genertor and to calculate percentage of votes for each candidate
	displayResults(c, v, p, n);//calling function to display results
}
void getVotes(int v[], int size)
{
	int min = 1500;
	int max = 25000;

	for (int i = 0; i<size; i++)
	{
		v[i] = (int)min + (rand() % (max - min + 1));//random number generator between 1500 and 25000
	}
}

void displayResults(string c[], int v[], double p[], int size)
{
	cout << "\nCandidates Votes \tPercent\n";
	int max = v[0];//to hold max number of votes
	int pos = 0;//which position in array has max number of votes which will be usefull to declare the winner

	for (int i = 0; i<size; i++)
	{
		if (max<v[i])
		{
			max = v[i];
			pos = i;
		}
		cout << "\n" << c[i];
		cout << "\t " << v[i] << "\t" << std::setprecision(2) << p[i] << "%";
	}
	cout << "\n Winner is " << c[pos] << " with " << v[pos] << " Votes";
}

void calculatePercents(int v[], double p[], int n)
{
	int sum = 0;//to hold sum of all candidates votes
	int min = 1500;
	int max = 25000;

	for (int i = 0; i<n; i++)
	{
		v[i]=(int) min + ( rand() % ( max - min + 1 ) );//random number generator between 1500 and 25000
		sum += v[i];
	}
	for (int i = 0; i<n; i++)
	{
		p[i] = ((double)v[i] / (double)sum) * 100;//calculating percentage of votes fro each candidate
	}
}

bool readFile(string c[], int *size)
{
	string candidate;
	ifstream in;
	in.open("candidates.txt");//opening file
	int i = 0;
	while(i<*size)
	while (in >> candidate)//reading till end of file
	{
		cout << candidate;
		c[i] = candidate;//assigning to array of candidate names
		i++;
	}
	*size = i;//assinging size of array
}
Last edited on
Oh, so close! The error message you get from the compiler tells you the line the error is on, and you nearly told us that as well. That would have been so helpful.

I'm going to guess the problem is here:
1
2
int n = 25;
string c[n];


n is not a const, and in C++ non-const sized arrays like this are forbidden. Make n a const.
Ok so changed line 16 to const int n = 25 but now I get error

'bool readFile(std::string [],int *)': cannot convert argument 2 from 'const int *' to 'int *'

in line 21
&n is of type "pointer to const int".

The function readFile expects a type "pointer to int".

See the difference? You have to pass the the function the right kind of parameter. Change readFile so it expects a const int*
Ok I changed to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;

bool readFile(string[], const int *);
void calculatePercents(int v[], double p[], int);
void displayResults(string c[], int v[], double p[], int);
void getVotes(int v[], int);

int main()
{
	const int n = 25;
	string c[n];
	int v[n];
	double p[n];
	getVotes(v, n);
	readFile(c, &n); //calling function to read candiadtes names from files
	calculatePercents(v, p, n);//to get votes using random number genertor and to calculate percentage of votes for each candidate
	displayResults(c, v, p, n);//calling function to display results 


I am getting more lost, trying to read articles after your help but not working now I get error C4716: 'readfile': must return a value, but saying line 86
Still having issues??
'readfile': must return a value


This one you really should be able to work out for yourself. readfile, that's a function. The error message is saying that the function readfile must return a value.

Do you understand what it means for a function "to return a value"? Is that something you know about?

bool readFile(string[], const int *);
What do you think bool means in that line of code?
Last edited on
Yes I understand what it means to return a value, and I understand bool is true or false but I am having trouble on the return value. It's driving me crazy trying to figure it out.
Yes I understand what it means to return a value,

Clearly not. You need to go back and learn about functions more.


Your function readFile says that it will return a bool. So do that.

Here is how to return true from a function:

return true;

Here is how to return false from a function:

return false;


Your function readFile says it will return a bool. So do that.


Topic archived. No new replies allowed.