Help with code!(please help me out)

Any help would be greatly appreciated. The program I am trying to write is a program that takes reads from a text file. In the txt file is five names and their votes. Im trying to get the total amount of votes and their percentage. I feel like I am close to doing it,but I keep getting errors in this section, as far as the 'percent' and the 'total':
1
2
3
4
5
6
7
8
9
 double percentage(double votes[], double total[])
{

	for(int i = 0; i<5; i++)
	{
		percent[i] = votes[i]/total)*100;
	}
	return percent;
} 


this is the whole 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
  #include<iostream>
#include<string>
#include<fstream>


using namespace std;

int read(string fname[], double votes[]);
double percentage(double votes[], double total[]);

int main()
{
	string name[5];
	double votes[5];
	double percent[5];
	string winner;
	
	
	ifstream in;
	in.open("data.txt");

	total = read(name, votes);
	winner = percentage(votes, total, percent);

	cout << name << [winner] << "is the winner!";
	

	if(!in)
	{
		cout << "File does not exist!" << endl;
	}


	return 0;


}

int read(ifstream in, string fname[], double votes[])
{
	double total = 0.0;
    int i = 0;


	while(!in.eof())
	{
		
	  in >> fname[i] >> votes[i];
	  total = total + votes[i];
	  ++i;
	}

	return total;

}

double percentage(double votes[], double total[])
{

	for(int i = 0; i<5; i++)
	{
		percent[i] = votes[i]/total)*100;
	}
	return percent;
}

int winner(double votes[])
{
	int winner = 0;
	
	for(int i = 0; i<5; i++)
	{

		if(votes[i] > votes[winner])
		{
			winner = i;
		}
		return winner;
	}
line 22 you use "total" without having defined it. Is it an int, a double, ... ?

The "percentage" function you're calling line 23 does not exist.

The existing "percentage" function has several problems :
- The variable "percent" is not defined in the function
- It's supposed to return a double, but if percent is an array, it's a double*
- The prototype of the "percentage" function defines total as an array. According to your code it's a single value, not an array.
- line 62 there's a missing parenthese
- It does not return the winner.

line 25, [winner] doesn't mean anything, I suppose you intended to write winner
I am trying to call the existing "percentage" function, I don't get why it isn't calling it.

On line 14, I defined 'total' as an array. Where in my code do I define it as a single value?

Also, 'total' on line 22 is a double.
Last edited on
line 23 the "percentage" function you're calling takes 3 arguments and returns a string.
The existing function takes 2 arguments and returns a double.

On line 14, I defined 'total' as an array.

line 14 is double votes[5];.

Also, 'total' on line 22 is a double

So is it an array or a double ?

Where in my code do I define it as a single value?

On line 22, according to you.
Last edited on
Damn dude, I'm a freakin idiot. Thanks for making me understand the mistakes I am making. I want 'total' to be an array, but every time I try to make 'total' an array it gives me an error.

In my 'percentage' function, what am I doing wrong with the 'percent' variable?

In my 'percentage' function, what am I doing wrong with the 'percent' variable?

"percent" is not declared in the function.
From what I understand it's supposed to be an array in which you store the percentage of the votes each candidate has. I also suppose that you intend to store the percentages in the "percent" array defined in your main().
That means you have to pass it as an argument to the function. The prototype should therefore be something like double percentage(double votes[], double total, double percent[]).

There is also the problem of the return value. The function is supposed to return a double, but you return "percent" which is not a double.
Personally I don't think this function should return anything.

A corrected version would be like this:
1
2
3
4
5
6
7
void percentage(double votes[], double total, double percent[])
{
	for(int i = 0; i<5; i++)
	{
		percent[i] = (votes[i]/total)*100;
	}
}

and the calling code in main() would be:
1
2
double total = read(name, votes);
percentage(votes, total, percent);
Thanks a lot, you're really helping me with this. But one more question, when I try to return 'total' in my 'read' function I get an error. Why is this?
This is what it looks like right now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int read(ifstream in, string fname[], double votes[], double total)
{
    int i = 0;

	while(!in.eof())
	{
		
	  in >> fname[i] >> votes[i];
	  total = total + votes[i];
	  ++i;
	}

	return total;
}
What is the error exactly ?
It tells me that I can't change 'total' from a double to a int. But I thought it should work as long as I had my function like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int read(ifstream in, string fname[], double votes[], double total)
{
    int i = 0;

	while(!in.eof())
	{
		
	  in >> fname[i] >> votes[i];
	  total = total + votes[i];
	  ++i;
	}

	return total;
}
Then convert total to an int: return (int)total;
But i want total to stay double. Every time I change one little thing I get some error.

I have no clue how to correct my errors because I don't know what I am doing wrong.



This is my whole code now:
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
#include<iostream>
#include<string>
#include<fstream>


using namespace std;

int read(ifstream in, string fname[], double votes[], double total);
void percentage(double votes[], double total, double percent[]);
int winner(double votes[]);

int main()
{
	string name[5];
	double votes[5];
	double percent[5];
	string winner;
	double total;
	
	
	ifstream in;
	in.open("data.txt");

	total = read(in, name, votes, total);
	percentage(votes, total, percent);
	
	

	cout << name << winner << "is the winner!";
	

	if(!in)
	{
		cout << "File does not exist!" << endl;
	}


	return 0;
}

 int read(ifstream in, string fname[], double votes[], double total)
{
    int i = 0;

	while(!in.eof())
	{
		
	  in >> fname[i] >> votes[i];
	  total = total + votes[i];
	  ++i;

	  cout << fname[i] << votes[i];
	}
	return total;
}

void percentage(double votes[], double total, double percent[])
{
	

	for(int i = 0; i<5; i++)
	{
		percent[i] = (votes[i]/total)*100;
	}
}

int winner(double votes[])
{
	int winner = 0;
	
	for(int i = 0; i<5; i++)
	{
		if(votes[i] > votes[winner])
		{
			winner = i;
		}
	}
	return winner;
}
If you want to return a double, then indicate that your function returns a double:
double read(ifstream in, string fname[], double votes[], double total)
Last edited on
Topic archived. No new replies allowed.