problems with arrays

I'm trying to write this code that takes in data from a txt file. I have to use parallel arrays to store the name of the voters the number of votes they received and the percentage of votes of each individual out of the total. I have to of the three arrays down so far. I tried to do percentage[5] = {votes[5] / total} but the program goes crazy so I'm kind of in a slump. Any ideas on what I'm doing wrong? Below is what is in the txt file.

Jonson 5000
Miller 4000
Duffy 6000
Robinson 2500
Ashtony 1800

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
 #include <iostream> 
#include <string>
#include <iomanip>
#include <conio.h>
#include <fstream>  

using namespace std;

void getData(ifstream &in, string name[], int votes[], double percentage[], int total);
void DisplayData(ofstream &out, string name[], int votes[], double percentage[], int total);

int main()
{
	int total = 0;
	string name[5] = {""};
	int votes[5] = {0};
	double percentage[5] = {votes[5] / total};
	

	ifstream in;
	in.open("input.txt");

	getData(in, name, votes,percentage,total);
	
	return 0;
}

void getData(ifstream &in, string name[], int votes[], double percentage[], int total)
{
	ofstream out;
	out.open("output.txt");

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

	DisplayData(out, name, votes, percentage, total);

}

void DisplayData(ofstream &out, string name[], int votes[], double percentage[], int total)
{
	cout << left << setw(20) << "Candidate" << left << setw(20) << "Votes Received" << "% of Total Votes" << endl; 
	out << left << setw(20) << "Candidate" << left << setw(20) << "Votes Received" << "% of Total Votes" << endl; 
	cout << endl;
	out << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << left << setw(25) << name[i] << votes[i] << percentage[i] << endl;
		out << left << setw(25) << name[i] << votes[i] << percentage[i] << endl;

	}

	cout << left << setw(25) << "Total:" << total << endl;
	out << left << setw(25) << "Total:" << total << endl;


}


I've also tried to just do votes[i] / total in the for loop but it doesn't display anything. I even tried to do votes[0] / total for each person but it only displays zeros.
Last edited on
Well, line 17 won't work:
 
    double percentage[5] = {votes[5] / total};
When that line is executed, total is zero, so there's a divide by zero error there. Best just to initialise that array with zero, like the votes.

The program seems constructed on the assumption that there will be exactly 5 lines of data in the input file, no more and no less. In my opinion, the program should be more flexible, perhaps allow for say 10 or even 20 candidates, and make the arrays big enough to store that many rows of data.

Then in function getData() you could count how many rows of data are actually read from the file. Well actually you are already doing that, the variable i acts as a count, but you don't do anything with that information. You might change function DisplayData() to accept an additional parameter, the count of the number of rows of data in the arrays.

But first there are two issues to deal with. We'll come to the percentages in a moment. There's another problem before we do that.

At line 34 while (!in.eof()) this while condition is not a good idea. Why, because it checks the condition of the input file before reading from it. What the program really needs to know is this: when the line in >> name[i] >> votes[i]; is executed, does it fail or succeed? You can only answer this question by testing the file after executing that line.
So, I recommend changing that code like this:
33
34
35
36
37
38
    int i = 0;
    while (in >> name[i] >> votes[i])
    {
        total = total + votes[i];
        i++;
    }

After that, as I said, we can use the value of i to tell us how many rows of data were read from the file. Now we get to calculate the percentages, like this:
1
2
3
4
    for (int k=0; k<i; k++)
    {
        percentage[k] = // here calculate percent from votes and total 
    }

And as i suggested, modify the DisplayData() function to accept one more parameter
 
void DisplayData(ofstream &out, string name[], int votes[], double percentage[], int total, int numRows)
and call it like this:
 
    DisplayData(out, name, votes, percentage, total, i);
and of course use the new parameter at line 51,
 
    for (int i = 0; i < numRows; i++)
Topic archived. No new replies allowed.