I have a couple of questions, regarding my program:
-----------------------------------------------------------
#1: Is it possible to have my table centered? The table is located towards the bottom of my console output and, as you can see, the data is sort of off center.
#2: Instead of having the user input five names and five int values, would it be possible to simply enter this data into my template at the bottom? If so, how would I go about doing that? To clarify: One may be able to (easily) create a loop which allows the user to input a name and integer value; however, in order for % of total votes to be calculated, all the data must be known.
------------------------------------------------------------
Program 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
|
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{int index=0,listsize=5,maxindex=1,votes[5];
float sum=0;
string candidates[5];
cout<<"Enter last name of candidate: "<<endl;
for(index=0;index<listsize;index++)
{cin>>candidates[index];}
cout<<endl;
cout<<"Enter votes received for corresponding candidate: "<<endl;
for(index=0;index<listsize;index++)
{cin>>votes[index];
sum=sum+votes[index];}
cout<<endl<<"Results of the election:"<<endl<<endl;
cout<<"Candidate"<<" "<<"Votes Received"<<" "<<"Percent of Total Votes"
<<endl<<endl;
for(index=0;index<listsize;index++)
{cout<<candidates[index]<<" "<<votes[index]<<" "
<<setprecision(4)<<
((votes[index]/sum)*100)<<endl;}
cout<<"Total "<<static_cast <int> (sum)<<endl<<endl<<endl;
for(index=0;index<listsize;index++)
{if(votes[maxindex]<votes[index])
maxindex=index;}
cout<<"The winner of the election is: "<<candidates[maxindex]<<"."<<endl;
cout<<"The winner was found at position "<<maxindex<<" in the array."<<endl;
system("PAUSE");
return EXIT_SUCCESS;}
| |
------------------------------------------------------------
My console output:
------------------------------------------------------------
Enter last name of candidate:
Johnson
Miller
Duffy
Robinson
Ashtony
Enter votes received for corresponding candidate:
5000
4000
6000
2500
1800
Results of the election:
Candidate Votes Received Percent of Total Votes
Johnson 5000 25.91
Miller 4000 20.73
Duffy 6000 31.09
Robinson 2500 12.95
Ashtony 1800 9.326
Total 19300
The winner of the election is: Duffy.
The winner was found at position 2 in the array.
Press any key to continue . . .