Help involving arrays and strings

Pages: 12
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.

closed account (E0p9LyTq)
You have one warning and one error.

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.
I have no idea what you mean, sadly.

Here are is my modified 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
#include <iostream>
#include <string>
#include <iomanip>

using namespace 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);

const int 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"



Last edited on
closed account (E0p9LyTq)
Line 71: cout << "And the winner is: " << candidates[findWinner(votes)] << endl;

You are calling findWinner() with 1 parameter, yet your function requires 2!
int findWinner(string candidates[], int votes[]);

Remove the string parameter from your prototype and your definition, you don't use the string array in the function.

Change line 67 to double percentage = calculatePercentage(votes, votes[i]);, and your warning will go away.
It works!

Thank you so, so much FurryGuy! I can't express the amount of gratitude I have for you, AbstractionAnon, and coder777.

I greatly appreciate it.
closed account (E0p9LyTq)
A nickel's worth of free advice:

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.
Topic archived. No new replies allowed.
Pages: 12