Candiate Programming- Percent issue

My programming professor is not very clear and makes me ashamed to ask questions lmao
So the program is "Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate's name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: (then it displays the candiate's names, votes, percentage of the total votes, and then shows who the winner is)"

I am having issues with apparently LNK2028, LNK2019, and LNK1120
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void getPercent(int votes, int totalVotes[], double percent[]);
void printResult(int total, string lastName[], int vt[], double percent[]);
int winInd(int vt[]);

const int CNDTS = 5;

int main()

{
string lastName[CNDTS];
int votes[CNDTS] = { 0 };
int totalVotes = 0;
double percent[CNDTS] = { 0.0 };

for (int i = 0; i < CNDTS; i++)
   {
   cout << "Enter the last name of the candidate" << (i+ 1) << " and  and the number of votes received: ";
		cin >> lastName[i] >> votes[i];

		totalVotes += votes[i];
	}


getPercent(totalVotes, votes, percent);

printResult(totalVotes, lastName, votes, percent);

system("pause");
		return 0;
}


	/* function to print the result*/

	void printResult(string lastName[], int votesTotal[], double percent[])
	{
		cout << "Candidate     Votes Received    % of Total Votes\n";
		for (int i = 0; i < CNDTS; i++)

			cout << left << setw(10) << lastName[i] /*creates the output grid*/
		     << right << setw(10) << votesTotal[i]
	     	 << right << setw(10) << percent[i]
			 << "\n";

		cout << "Total" << votesTotal << "\n";
		cout<< "The winner of the election is: "<< lastName[winInd(votesTotal)];
	}
	
	int winInd(int votesTotal[])
	{
		int largest = 0;
		for (int i = 0; i < CNDTS; i++)
			if (largest < votesTotal[i]) /*will find the largest sum*/
				largest = i;
		return largest;
	}





	/*function to calculate the percentages*/
	void getPercent(int votes, int totalVotes[], double percent[])
	{
		for (int i = 0; i < CNDTS; i++)
		{ /*Forumla to find the percent*/
			percent[i] = static_cast<double>(totalVotes[i]) / static_cast<double>(votes) * 100;
		}

	}
Last edited on
Hey. Please next time copy paste your errors to make it much easier on us.

Function prototype:

void printResult(int total, string lastName[], int vt[], double percent[]);

Function definition:
void printResult(string lastName[], int votesTotal[], double percent[])

They have to match.
Line 39,void printResult(string lastName[], int votesTotal[], double percent[]) has only three arguments, but your function prototype on line 6, void printResult(int total, string lastName[], int vt[], double percent[] has four arguments.

PS: TarikNeaj beat me to it :)
Last edited on
Thank you both.

No more errors, but I cannot seem to get the "getPercent" function to work.
I'd copy paste but I just realized this forum doesn't have an image insertion.
Anyway...

The output is looking like this:

"Candidate Votes Received % of Total Votes
a 100 27.1739
b 200 54.3478
c 50 13.587
d 6 1.63043
e 12 3.26087
Total0032F218
"

I'm sure it's my math with the getPercent that is messing it up, but I'd like some feedback and help
I'm sure it's my math with the getPercent that is messing it up, but I'd like some feedback and help


getPercent looks fine to me.
Line 49 displays the address of the array.
winInd... not so good. Why are you comparing an index to an element of the array?
That was the professor's suggestion.

It still runs fine (winInd), and I'm more concerned about the getPercent function, since the total is coming out wrong.
I'm more concerned about the getPercent function, since the total is coming out wrong.

The getPercent function is fine, and has absolutely nothing to do with calculating the total.

Did you, perhaps, see that line 49 displays the address of an array? Isn't that where you're trying to output the... total? I think I mentioned that somewhere before.


It still runs fine (winInd)

It may run fine, but it won't give you correct results.



Okay, cool. Thank you.
Below is a suggestion

From:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* function to print the result*/

	void printResult(string lastName[], int votesTotal[], double percent[])
	{
		cout << "Candidate     Votes Received    % of Total Votes\n";
		for (int i = 0; i < CNDTS; i++)

			cout << left << setw(10) << lastName[i] /*creates the output grid*/
		     << right << setw(10) << votesTotal[i]
	     	 << right << setw(10) << percent[i]
			 << "\n";

		cout << "Total" << votesTotal << "\n";
		cout<< "The winner of the election is: "<< lastName[winInd(votesTotal)];
	}
	
	int winInd(int votesTotal[])
	{
		int largest = 0;
		for (int i = 0; i < CNDTS; i++)
			if (largest < votesTotal[i]) /*will find the largest sum*/
				largest = i;
		return largest;
	}


To:

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
void printResult(int total, string lastName[], int votesTotal[], double percent[])
{
    cout << "Candidate     Votes Received    % of Total Votes\n";
    for (int i = 0; i < CNDTS; i++)
    {

	   cout << left << setw(10) << lastName[i] /*creates the output grid*/
		  << right << setw(15) << votesTotal[i]
		  << right << setw(15) << percent[i]
		  << "\n";
    }

    cout << "Total " << total << "\n"; // It obtains the total from the main function (line 24 on main)
    cout << "The winner of the election is: " << lastName[winInd(votesTotal)];

}

int winInd(int votesTotal[])
{
    int largest = 0;
    int position = 0;
    for (int i = 0; i < CNDTS; i++)
    {
	   // If it finds the largest value, then record the larfest variable
	   // and record its position by recording the index.
	   if (votesTotal[i] > largest) /*will find the largest sum*/ 
	   {
		  largest = votesTotal[i];
		  position = i;
	   }

    }
    return position; // Return the index of the largest value.
}
Topic archived. No new replies allowed.