Can anyone help me with my arrays please?

Apr 16, 2013 at 1:13am
I must use functions and arrays and I am having issues and cant get my code to run. I have even put it through codepad and I am not really understanding what I am doing wrong. Here is waht codepad has to say:
In function 'int main()':
Line 53: error: invalid types 'int[int]' for array subscript
compilation terminated due to -Wfatal-errors.

The program I'm writting lettes the user enter 5 names and their votes. Its suppose to give the total votes the names entered and the percentage of votes each person had. Here is my code so far. As always I will use your screen name to give credit for helping me. Thank you in advance.

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

using namespace std;

// function prototype

void intro() ; // good to go
void initialize(int arr[], int size);


const int ARRAY_SIZE = 5 ;

int main()
{
	string candidateName[ARRAY_SIZE] ;
	int votesReceived[ARRAY_SIZE] ;
	int Percentage[ARRAY_SIZE] ;
	
	
	int totalVotes, votePercentage, winner ;
	
	intro() ; // calls the intro

	cout << "Please enter the candidate's name and the number of votes" << endl;
	
	// inputs candidate and votes
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		cin >> candidateName[i] >> votesReceived[i];
	}
	
	
	// calculates percentage of votes
	votePercentage = 0 ;
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		votePercentage = (votesReceived[i] / totalVotes) * 100 ; 
	}

		// finds the winner
		winner = 0 ;
		for (int i = 0; i < ARRAY_SIZE; i++)
		{
			if (votesReceived[i] > winner)
				winner = votesReceived[i] ;
		}

	// out put results
		cout << "Candidate" << " " << "Votes Received" << "" << "Percent of Total Votes" << endl ;
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		cout << candidateName[i] << votesReceived[i] << votePercentage[i] << endl ;
	}
	
	cout <<"The winner is" << candidateName[winner] << endl ;



	system ("PAUSE") ;

	return 0;
	}

// functions

void intro()
{
	cout << "This program allows the user to input five candidates and their votes received\n in the local election." << endl << endl ;
}

void initialize(int arr[], int size)
{
	for (int i = 0; i < size; i++)
	{
		arr[i] = 0;
	}
}

void total(const int arr[], int size)
{
		// find total votes
	int totalVotes = 0 ;
	for (int i = 0; i < ARRAY_SIZE; i++)
	{  
		totalVotes += arr[i] ;
	}

	cout << "Total number of votes is" << totalVotes << endl;
}
Apr 16, 2013 at 2:06am
check your votePercentage its not an array...
Apr 16, 2013 at 2:32am
Thank you I corrected it and I'm still having issues. I can now run it and enter the names and number of votes but it crashes after that. I will work on it some more before I ask for more help.
Apr 16, 2013 at 3:05am
Ok Im having issues with my persentages and Ive been working on them and I'm messing up somewhere. here is my updated 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
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


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// function prototype

void intro() ; // good to go
void initialize(int arr[], int size) ;
//void total(const int arr[], int size) ;


const int ARRAY_SIZE = 5 ;

int main()
{
	string candidateName[ARRAY_SIZE] ;
	int votesReceived[ARRAY_SIZE] ;
	int percentage[ARRAY_SIZE] ;
	
	
	
	intro() ; // calls the intro

	cout << "Please enter the candidate's name and the number of votes" << endl << endl ;
	
	// inputs candidate and votes
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		cin >> candidateName[i] >> votesReceived[i];
	}
	
	// find total votes
	int totalVotes = 0 ;
	for (int i = 0; i < ARRAY_SIZE; i++)
	{  
		totalVotes += votesReceived[i] ;
	}

	// calculates percentage of votes
	int votePercentage = 0 ;
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		votePercentage = (votesReceived[i] / totalVotes) * 100 ; 
	}

		// finds the winner
		int winner = 0 ;
		for (int i = 0; i < ARRAY_SIZE; i++)
		{
			if (votesReceived[i] > winner)
				winner = votesReceived[i] ;
		}

	// out put results
	cout << "Candidate" << " " << "Votes Received" << "" << "Percent of Total Votes" << endl << endl ;
	
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		cout << candidateName[i] << votesReceived[i] << percentage[i] << endl ;
	}
	
	cout << "Total " << totalVotes << endl ;
	cout << "The winner is " << candidateName[winner] << endl ;


	system ("PAUSE") ;

	return 0;

}

// functions

void intro()
{
	cout << " This program allows the user to input five candidates and their votes received\n in the local election." << endl << endl ;
}


void initialize(int arr[], int size)
{
	for (int i = 0; i < size; i++)
	{
		arr[i] = 0;
	}
}
/*
void total(const int arr[], int size)
{
		// find total votes
	int totalVotes = 0 ;
	for (int i = 0; i < ARRAY_SIZE; i++)
	{  
		totalVotes += arr[i] ;
	}

	cout << "Total number of votes is" << totalVotes << endl;
} */


Last edited on Apr 16, 2013 at 3:06am
Apr 16, 2013 at 3:48am
Hint ^^
[] line 47 and 63
[] check your finds the winner

and try to look the difference between "1" and "1.0"
Last edited on Apr 16, 2013 at 3:50am
Apr 24, 2013 at 1:51pm
Thank you for the help screen name will be used to give credit for the help.
Topic archived. No new replies allowed.