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
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
void getPercent(int votes, int totalVotes[], double percent[]);
void printResult(int total, string lastName[], int vt[], double percent[]);
int winInd(int vt[]);
constint 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;
}
}
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.
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?
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.
/* 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;
}
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.
}