I'm creating a voting program in which you input the persons name and how many votes they have received. It than prints out the persons name, their amount of votes and the percentage of their votes from the total.
I can't seem to get the percent of total to work correctly, everything else is working fine. I'm also aware that nothing lines up correctly, i'll be fixing that after I get it working.
Any ideas? thanks.
#include <iostream>
#include <iomanip>
#include <String>
using namespace std;
int sumVotes(int list[]);
int winnerIndex(int list[]);
int votes[20];
double percent[10];
int main()
{
string name[10];
int i;
for(i=0; i<5; i++)
{
cout << "Enter the name of the candidate and the number of votes: ";
cin >> name[i];
cin >> votes[i];
}
int totalvote;
totalvote = sumVotes(votes);
int w;
w = winnerIndex(votes);
cout << "Name" << setw(25) << "Votes Receieved" << setw(25) << "% of Total" << endl;
for(i=0; i<5; i++)
{
percent[i] = totalvote;
cout << name[i] << setw(21) << votes[i] << setw(20) << percent[i] << endl;
}
cout << endl << setw(30) << "Total: " << totalvote;
return 0;
}
int sumVotes(int votes[])
{
int i;
int total = 0;
for(i=0; i<5; i++)
total = total + votes[i];
return total;
}
int winnerIndex(int votes[])
{
int i;
int maximum;
maximum = votes[0];
for(i=0; i<5; i++)
{
if(votes[i] > maximum)
maximum = votes[i];
}
return maximum;
}
Output:
Enter the name of the candidate and the number of votes: Person1 5000
Enter the name of the candidate and the number of votes: Person2 4000
Enter the name of the candidate and the number of votes: Person3 6000
Enter the name of the candidate and the number of votes: Person4 2500
Enter the name of the candidate and the number of votes: Person5 1800
Name Votes Receieved % of Total
Person1 5000 0
Person2 4000 0
Person3 6000 0
Person4 2500 0
Person5 1800 0