Hi everyone this is what I have so far for the following problem so far its working correctly I just want to know how to move the numbers to display under the % of total votes? I also want to add a total of all the votes?
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.
This program must be done using parallel arrays to store the name and number of votes for each candidate.
The number must be formatted appropriately as shown in the sample output below.
#include <string>
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
string names[5];
int votes[5];
int winner;
int total = 0;
string winner_name;
cout << "Enter candidate's name and the votes received by the candidate." << endl;
for (int i = 0; i<5; i++)
{
cin >> names[i];
cin >> votes[i];
total += votes[i];
}
winner = votes[0];
//This for loop will decides the winner based on the votes
for (int j = 0; j<5; j++)
{
if (votes[j]>winner)
{
winner = votes[j];
winner_name = names[j];
}
}
std::cout << std::setprecision(2) << std::fixed;
cout << "Candidate\tVotes Received\t% of Total Votes" << endl;
for (int i = 0; i<5; i++)
{
cout << setw(10) << names[i] << "\t" << votes[i] << "\t"<< ((double)votes[i] / total) * 100 << endl;
}
cout << "The Winner of the Election is " << winner_name << endl;
system("PAUSE");
return 0;
}
In line 48 you can use a combination of std::setfill(' ') and std::setw(n) to space out what you print. std::setfill() will fill in the unused portion of the std::setw(n) width.