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.
#include <iostream>
#include <string>
usingnamespace std;
// function prototype
void intro() ; // good to go
void initialize(int arr[], int size);
constint 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(constint 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;
}
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.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
// function prototype
void intro() ; // good to go
void initialize(int arr[], int size) ;
//void total(const int arr[], int size) ;
constint 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;
} */