Line 67: This is just a warning. calculatePercentage returns a double. percentage is declared an int. You're going to lose decimal places when you assign a double to an int. You may want to change percentage to a double.
Line 71: Your prototype for findWinner at line 9 says it takes two arguments. However, your implementation at line 77 says it takes only one argument. Change your prototype to agree with your implementation.
The error is because you have a function prototype that is different than your function definition.
int findWinner(string candidates[], int votes[]);
vs.
int findWinner(int votes[])
Your warning is from this line of code:
int percentage = calculatePercentage(votes, votes[i]);
Your function returns a double and you are assigning it to an int. Either change the return type of your calculatePercentage() function to int, or change the type of your percentage variable to double.
A warning doesn't prevent your source from compiling, it does. Your compiler is merely letting you know you are possibly changing the value because you are assigning it to a completely different type.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
//function prototype
void inputCandidates(string candidates[], int votes[]);
int findWinner(string candidates[], int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);
constint Number_Of_Candidates = 5;
int main()
{
string candidates[Number_Of_Candidates];
int votes[Number_Of_Candidates];
inputCandidates(candidates, votes);
printResults(candidates, votes);
system("pause");
return 0;
}
//inputCandidates takes input of candidates names
//and the number of votes they have received.
void inputCandidates(string candidates[], int votes[])
{
cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";
for (int i = 0; i < Number_Of_Candidates; i++)
{
cin >> candidates[i] >> votes[i];
}
}
//end of inputCandidate
//calculatePercentage returns the number of votes in the array as percentages
//for respective candidates
double calculatePercentage(int votes[], int vote)
{
int sumOfVotes = 0;
for (int i = 0; i < Number_Of_Candidates; i++)
{
sumOfVotes += votes[i];
}
double percentage = static_cast<double>(vote) / sumOfVotes;
return percentage * 100;
}
//end of calculatePercentage
//print results of the array in desired format, and then displays the winning candidate.
void printResults(string candidates[], int votes[])
{
cout << "Name:" << setw(15) << "Votes:" << setw(15) << "Percentage:" << endl;
for (int i = 0; i < Number_Of_Candidates; i++)
{
cout << candidates[i] << setw(15) << votes[i] << setw(15);
int percentage = calculatePercentage(votes, votes[i]);
cout << percentage << "%" << endl;
}
cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
}
//end of printResults
//analyzes the array of candidates and corresponding vote values, and
//then determines winner by which candidate had the highest vote value
int findWinner(string candidates[], int votes[])
{
int winner = 0;
for (int i = 0; i < Number_Of_Candidates; i++)
{
if (votes[i] > votes[winner])
winner = i;
}
return winner;
}
//end of findWinner
and still build error.
In fact, now there are more:
" 3 IntelliSense: argument of type "int *" is incompatible with parameter of type "std::string *" e:\CompSci_1\HW_Assignments\Candidate analysis\Candidate analysis\candidate analysis.cpp 77 57 Candidate analysis"
" 4 IntelliSense: too few arguments in function call e:\CompSci_1\HW_Assignments\Candidate analysis\Candidate analysis\candidate analysis.cpp 77 62 Candidate analysis"
"Error 2 error C2660: 'findWinner' : function does not take 1 arguments e:\compsci_1\hw_assignments\candidate analysis\candidate analysis\candidate analysis.cpp 77 1 Candidate analysis"
The error messages you posted were quite clear where and what the problems were. When getting multiple errors/warnings focus on the first one, try to understand what the compiler is telling you and try to fix that one problem. More often than not fixing one problem solves other problems.
Trying to fix errors is an art, it takes time and a lot of patience. Some times the best solution is to "walk away" for a bit, take a break, and come back to the code with rested/refreshed eyes.