Parallel Arrays CandidateVotes Please help

So im doing this Candidate Vote in which the name and the vote are gathered from a file.txt and then shows the percentage of each candidate and then display the winner "indexofMax"


but it look like it does not want to compile..... The functions might be horribly coded thats because Arrays is not my strong T_T

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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;

int sumList(const int list[], int size);
int indexOfMax(const int list[], int size);
// This function gets the results from the file and returns them in the corresponding
void getElectionData(ifstream & iFile, string candidates[], int ballots[], int & count);
void printResults(const string candidates[], const int ballots[], int count, int totalBallots);
const int NUM_CANDIDATES = 10; // The number of candidates

int main()
{
	ifstream inFile;
	string names[NUM_CANDIDATES]; // The names of the candidates
	int votes[NUM_CANDIDATES]; // The number of votes received by each candidate
	int totalVotes; // The total number of votes received by all candidates
	int max;	// number of candidates processed

	inFile.open("votingresults.txt");

	inFile.open("votingresults.txt");
	if (!inFile)
	{
		cout << "File not found!" << endl << endl;
		system("pause");
		return 1;
	}


	
    ///////////////////////////////
    // Start of your code


    // Get the candidates' names and number of votes each received
    // Assume the user enters valid input (just call the getElectionData() function)
	getElectionData(inFile, names, votes, max);
	

    // Calculate the total number of votes received by all candidates
    // (call the sumList() function)
	totalVotes = sumList(votes ,max);
	

    // Print each candidate's name, the number of votes she received,
    // and the percent of votes the candidate received
	// (just call the printResults() function)
	printResults(names, votes, max, totalVotes);
	

	inFile.close();
    // End of your code in the main function
    // Don't forget to complete the functions below
    ///////////////////////////////

    return 0;
}

//
// Returns the sum of the numbers in the given array
//
// WRITE THE BODY OF THIS FUNCTION
//
int sumList(int list[], int size)
{
	int i, sum = 0;
	for(i = 0; i<size; i++)
	{
		sum = list[i] + sum;
	}
	return sum;
}

//
// Returns the index of the largest value in the given array (not the largest
// value, but the index of it)
//
// WRITE THE BODY OF THIS FUNCTION
//
int indexOfMax(int list[], int size)
{
	int i;
	int Max = 0, maxIndex= 0;
	for(i = 0; i < size; i++) // loop 10 times
	{
		if(Max < list[i]) //if Max is lkess then the index it will store the new index as max 
		{
			Max = list[i];
			maxIndex = i;
		}
	}
	return maxIndex;
}

// Gets the names of the candidates and the votes they got from the file and returns
// them in the corresponding arrays. It also returns the number of rows in the file that
// were processed
//
// WRITE THE BODY OF THIS FUNCTION
//
void getElectionData(ifstream & iFile, string candidates[], int ballots[], int & count)
{
	int i = 0;
	while(iFile >> candidates[i] >> ballots[i])
	{
		count = count + ballots[i];
		i++;
	}
}

// Receives the candidates names and the quantity of votes they got, the number of elements
// in the arrays to be processed, and the total number of votes so they can be displayed as
// shown in the file with the problem statement
// Must call the indexOfMax function() to find the array index of the candidate with the
// largest number of votes
//
// WRITE THE BODY OF THIS FUNCTION
//
void printResults(const string candidates[], const int ballots[], int count, int totalBallots)
{
	int k;
	cout << "Candidate" << setw(20) << "Num Votes" << setw(30) << "% of votes" << endl; 
	for(int i = 0; i<count; i++)
	{
		cout << candidates[i] << setw(20) << ballots[i] << setw(20) << totalBallots/count /*Goes here */<< endl;
	}
	k = indexOfMax(ballots, totalBallots);
	cout << "The winner is: " << candidates[k] << endl << endl;
}
closed account (48T7M4Gy)
This now compiles but I don't have the text files to fully test. There were only a few small changes required to get this far and your can track them through by comparing to the OP.

(I've removed most of the comments as they are redundant rubbish.)

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

using namespace std;

int sumList(const int[], int);
int indexOfMax(const int[], int);

void getElectionData(ifstream&, string[], int[], int& );
void printResults(const string[], const int[], int, int);
const int NUM_CANDIDATES = 10;

int main()
{
    ifstream inFile;
    string names[NUM_CANDIDATES];
    int votes[NUM_CANDIDATES];
    int totalVotes;
    int max;
    
    inFile.open("votingresults.txt");
    
    inFile.open("votingresults.txt");
    if (!inFile)
    {
        cout << "File not found!" << endl << endl;
        system("pause");
        return 1;
    }
    
    getElectionData(inFile, names, votes, max);
    totalVotes = sumList(votes ,max);
    printResults(names, votes, max, totalVotes);
    
    inFile.close();
    
    return 0;
}

int sumList(const int list[], int size)
{
    int i, sum = 0;
    for(i = 0; i<size; i++)
    {
        sum = list[i] + sum;
    }
    return sum;
}

int indexOfMax(const int list[], int size)
{
    int i;
    int Max = 0, maxIndex= 0;
    for(i = 0; i < size; i++) 
    {
        if(Max < list[i])
        {
            Max = list[i];
            maxIndex = i;
        }
    }
    return maxIndex;
}

void getElectionData(ifstream & iFile, string candidates[], int ballots[], int & count)
{
    int i = 0;
    while(iFile >> candidates[i] >> ballots[i])
    {
        count = count + ballots[i];
        i++;
    }
}

void printResults(const string candidates[], const int ballots[], int count, int totalBallots)
{
    int k;
    cout << "Candidate" << setw(20) << "Num Votes" << setw(30) << "% of votes" << endl;
    for(int i = 0; i<count; i++)
    {
        cout << candidates[i] << setw(20) << ballots[i] << setw(20) << totalBallots/count /*Goes here */<< endl;
    }
    k = indexOfMax(ballots, totalBallots);
    cout << "The winner is: " << candidates[k] << endl << endl;
}
Last edited on
i like the word rubbish... but anyways It did compile THANKS alot it did not worked how its supposed to but that my fault ^_^ thanks again
closed account (48T7M4Gy)
Come back if you need help because only you know what perfect operation is.

(
As far as the comments are concerned if you go for sel-documenting code, desriptive variable and function names etc beyond an overall explanation at the head of the program most comments are just that ... rubbish. They get in the way of being able to read what is going on too. :)
)
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
84
85
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace std;

int sumList(const int[], int);
int indexOfMax(const int[], int);
void getElectionData(ifstream&, string*, int*, int&);
void printResults(const string[], const int[], int, int);

const int NUM_CANDIDATES = 10;

int main()
{
    ifstream inFile;
    string names[NUM_CANDIDATES];
    int votes[NUM_CANDIDATES];
    int totalVotes;
    int max;
    
    inFile.open("votingresults.txt");
    if (!inFile)
    {
        cout << "File not found!" << endl << endl;
        system("pause");
        return 1;
    }
    
    getElectionData(inFile, names, votes, max);
    totalVotes = sumList(votes, max);
    
    printResults(names, votes, max, totalVotes);
    
    inFile.close();
    
    return 0;
}

int sumList(const int list[], int size)
{
    int sum = 0;
    for(int i = 0; i < size; i++)
    {
        sum = list[i] + sum;
    }
    return sum;
}

int indexOfMax(const int list[], int size)
{
    
    int Max = 0, maxIndex= 0;
    for(int i = 0; i < size; i++)
    {
        if(Max < list[i])
        {
            Max = list[i];
            maxIndex = i;
        }
    }
    return maxIndex;
}

void getElectionData(ifstream & iFile, string candidates[], int ballots[], int& count)
{
    count = 0;
    while(iFile >> candidates[count] >> ballots[count])
    {
        count++;
    }
}

void printResults(const string candidates[], const int ballots[], int count, int totalBallots)
{
    cout << " Candidate           Num Votes          % of votes" << endl;
    for(int i = 0; i < count; i++)
    {
        cout << setw(10) << candidates[i] << setw(20) << ballots[i] << setw(20) << 100*ballots[i]/(double)totalBallots << endl;
    }
    
    int k = indexOfMax(ballots, count);
    cout << "The winner is: " << candidates[k] << endl << endl;
}
Topic archived. No new replies allowed.