You are here:

C++/Help With Election code

Advertisement


Question
QUESTION: I cannot get this to work is there anyway you can help. Thanks again for offering such a great service

The question is

Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate.  The program should then output each candidate's name, the votes received by that candidate, 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:

Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Anthony 1800 9.33
Total 19300
The Winner of the Election is Duffy.

The Code
_______________________________________________________

#include <iostream>
#include <iomanip>


using namespace std;

int main();

{

char names[N][20];
int votes[N];
int totalVotes;
int winnerPos;  
int winnerVotes;


void get_input(){
int i;
totalVotes = 0;
for (i=0;i     cout << "Enter Last Name of Candidate" << endl;
    cin >> names[i];  
    cout << "Enter Number of Votes received " << endl;
    cin >> votes[i];  
    totalVotes += votes[i];  
}

}

void calculate_winner(){
int i;
winnerPos = 0;
winnerVotes = votes[0];  


for (i=1;i    if ( winnerVotes < votes[i] )
{
      winnerPos = i;
      winnerVotes = votes[i];
   
}
}
void show_output(){
int i;
cout <<"Candidate"<<"tt"<<"Votes Received"<for ( i = 0; i < N; i++ ){
   cout.width(9);
   cout << left<}
cout.width(9);
cout <<"Total "<cout <<"The Winner of the Election is "<
}

int main(){
get_input();
calculate_winner();
show_output();

return 0;
}

ANSWER: hi dear     Andrea !!

Ive done some corrections with your code , kindly check out your programme ,revert me in case of issues !

   #include <iostream>
#include <iomanip>


using namespace std;
#define N 5

  char names[N][20];
  int votes[N];
  int totalVotes;
  int winnerPos;  
  int winnerVotes;


  void get_input(){
     int i;
     totalVotes = 0;
     for (i=0;i<N;i++)
     {
        cout << "Enter Last Name of Candidate" << endl;
        cin >> names[i];  
        cout << "Enter Number of Votes received " << endl;
        cin >> votes[i];  
        totalVotes += votes[i];  
     }


}

void calculate_winner()
{
  int i;
  winnerPos = 0;
  winnerVotes = votes[0];  


  for (i=1;i<N;i++)
  if ( winnerVotes < votes[i] )
  {
     winnerPos = i;
     winnerVotes = votes[i];

  }
}
void show_output(){
  int i;
  cout <<"Candidate"<<"tt"<<"Votes Received"<<endl;
  for ( i = 0; i < N; i++ )
     cout<<names[i]<<"  "<<votes[i]<<endl;
  cout<<endl;
  
  cout<<"Winner of the election "<<names[winnerPos];
}

int main(){
  get_input();
  calculate_winner();
  show_output();

  return 0;
}


Thanks and Regards !
Prince M. Premnath


---------- FOLLOW-UP ----------

QUESTION: Thanks for your help sir. Just a few more quick ?s could you please help me line these up so it looks better and I also need to add a % of total votes column but I cannot figure it out. Took me forever to write this code any help is appreciated thanks

Answer
Hi Dear Andrea !
Ive made some corrections with your code to work as you expected , check out your programme get back to me in case of issue!


#include <iostream.h>
#include <iomanip.h>



#define N 5

char names[N][20];
int votes[N];
float percent[N];
int totalVotes;
int winnerPos;  
int winnerVotes;


void get_input(){
int i;
totalVotes = 0;
for (i=0;i<N;i++)
{
cout << "Enter Last Name of Candidate" << endl;
cin >> names[i];  
cout << "Enter Number of Votes received " << endl;
cin >> votes[i];  
totalVotes += votes[i];  
}


}

void calculate_winner()
{
int i;
winnerPos = 0;
winnerVotes = votes[0];  


for (i=1;i<N;i++)
if ( winnerVotes < votes[i] )
{
winnerPos = i;
winnerVotes = votes[i];

}
}
void show_output(){
int i;
cout<<setiosflags(ios::left)<<setw(20)<<"Candidate"
  <<"Votes Rec"<<setiosflags(ios::left)<<setw(12)<<"% Of Total Votes"<<endl;
for ( i = 0; i < N; i++ )
   cout<<setiosflags(ios::left)<<setw(20)<<names[i]
  <<setw(12)<<votes[i]
  <<setw(10)<<((float)votes[i]/(float)totalVotes)*100.0<<"%"
  <<endl;
cout<<endl;

cout<<"Winner of the election "<<names[winnerPos];
}

int main(){
get_input();
calculate_winner();
show_output();

return 0;
}

Thanks and Regards !
Prince M. Premnath  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.