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':
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
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.
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;
}
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;
}
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)