this program is to tally the votes from total and each of four counties for two candidates.As well displaying the winner. The user has to input the votes for each county and the candidates. Now, thank to you guys, I worked out the kinks for the purpose stated above. But now, I want to add the Cout statements that will allow the user to specify the names of four counties and two candidates. as well using the names for reporting the result.
I am not sure how to incorporate the variables in the loop that will allow to print the names while reporting results like the overall winner, and the winner in each counties.
I appreciate your time!
#include<iostream>
using namespace std;
int tier1();
int main(void)
{
int return_val = tier1();
if (return_val < 0) // print an error
return 0;
}
int tier1(){
int votes[8];
int i, j, N; // variables
int k = 0;
for (i=0; i<4; i++)
{
cout << "county" << i << "\n"; // lists the 4 counties/candidates
for (j=0; j<2; j++)
{
cout << "How many votes did the candidate " << j << " get?\n";
N=0;
cin >> N;
votes[k++] = N;;
}
if (votes[k-2] + votes[k-1] > 100) //checking if it goes over 100 votes
{
cout << "One of the counties has too many votes. Exiting!\n"; // Print an error
exit(1);
}
}
int candidateOneVotes = 0; //resetting
int candidateTwoVotes = 0;
for (i = 0; i < 8; i = i+2)
{
cout << votes[i] << "\n";
cout << votes[i+1] << "\n";
candidateOneVotes += votes[i];
candidateTwoVotes += votes[i+1];
}
if (candidateOneVotes > candidateTwoVotes){
cout << "The winner of the election is " << c0 << "\n";
}
else
{
cout << "The winner of the election is " << c1 << "\n";
}
cout << "Here is the voting results:\n";
cout << c0 << " got ";
cout << candidateOneVotes;
cout << " votes\n ";
cout << c1 << "got ";
cout << candidateTwoVotes;
cout << " votes ";
return 0;
}
First you'll want to pull in the contents of the string and vector headers.
Next you'll want to update the declaration of
tier1
to accept two strings and a vector as aguments. This will allow you to pass the first and second candidate names along with a list of counties.Now you'll want to update
main
to read the candidates and county names fromcin
and pass those names to thetier1
function.Now you'll want to update the definition of
tier1
to accept two string arguments containing the candidate names and the vector containing the county names. Rather than relying on magic numbers to determine the number of counties you can call thesize
member function of the vector.