The calculation is only right in a and c, it is very wrong on b and c is off by 1. And the percentage only shows zero. Although the formulas is the same I can't figure out why the two is wrong, And why the percentage only shows zero and doesn't follow my formula.
Here the output enter image description here
And here is my code
#include <iostream>
using namespace std;
int main(){
string candidate[4];
int a[5], b[5], c[5], d[5];//precint
int at,bt,ct,dt;
int ap, bp, cp, dp;
int total;
int choice;
char yesNo;
int i;
do{
cout<<"[1] Enter candidates names and votes"<<endl;
cout<<"[2] Show Tally"<<endl;
cout<<"[3] Exit"<<endl;
cout<<"Enter a choice: ";
cin>>choice;
switch(choice){
case(1):
for(int i=0; i<4; i++){
cout<<"Enter candidate names: ";
cin>>candidate[i];}
for(int i=0; i<5; i++){
cout<<"Candidate "<<candidate[0]<<" precinct "<<i+1<<" votes: ";
cin>>a[i];
}
for(int i=0; i<5; i++){
cout<<"Candidate "<<candidate[1]<<" precinct "<<i+1<<" votes: ";
cin>>b[i];
}
for(int i=0; i<5; i++){
cout<<"Candidate "<<candidate[2]<<" precinct "<<i+1<<" votes: ";
cin>>c[i];
}
for(int i=0; i<5; i++){
cout<<"Candidate "<<candidate[3]<<" precinct "<<i+1<<" votes: ";
cin>>d[i];
}
break;
case(2):
cout<<"Precinct\t"<<candidate[0]<<"\t\t"<<candidate[1]<<"\t\t"<<candidate[2]<<"\t\t"<<candidate[3]<<endl;
for(int i=0; i<5; i++){ //for displaying the tally
cout<<i+1<<"\t\t"<<a[i]<<"\t\t"<<b[i]<<"\t\t"<<c[i]<<"\t\t"<<d[i]<<endl;
}
cout<<endl;
for(int i=0; i<5; i++){ //for the total votes
at=at+a[i];
bt=bt+b[i];
ct=ct+c[i];
dt=dt+d[i];
}
total=at+bt+ct+dt; //for the percent
ap=(at/total)*100;
bp=(bt/total)*100;
cp=(ct/total)*100;
dp=(dt/total)*100;
cout<<"Candidate "<<candidate[0]<<" total votes and percentage: "<<at<<" votes. "<<ap<<"%"<<endl;
cout<<"Candidate "<<candidate[1]<<" total votes and percentage: "<<bt<<" votes. "<<ap<<"%"<<endl;
cout<<"Candidate "<<candidate[2]<<" total votes and percentage: "<<ct<<" votes. "<<ap<<"%"<<endl;
cout<<"Candidate "<<candidate[3]<<" total votes and percentage: "<<dt<<" votes. "<<ap<<"%"<<endl;
break;
}
cout<<"Do you want to continue(Y/N): ";
cin>>yesNo;
}while (yesNo == 'y' || yesNo == 'Y');
cout<<"Thank You!";
return 0;
}