how to allow the user to specify the names and the program will use that names for reporting results?

88 views Asked by At

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;

}
1

There are 1 answers

5
Captain Obvlious On BEST ANSWER

First you'll want to pull in the contents of the string and vector headers.

#include <string>
#include <vector>

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.

int tier1(
    const std::string& c0,
    const std::string& c1,
    const std::vector<std::string>& counties);

Now you'll want to update main to read the candidates and county names from cin and pass those names to the tier1 function.

int main()
{
    cout << "Welcome to the VoteTime Program. Please do what it tells you.\n";

    std::string c0;
    cout << "Please name the candidate 0. (For example: Bob)\n";
    std::getline(cin, c0);

    std::string c1;
    cout << "Please name the candidate 1. (For example: John)\n";
    std::getline(cin, c1);

    std::vector<std::string> counties;
    for (int i = 0; i < 4; ++i)
    {
        std::string county;
        cout << "Please name county #" << i << '\n';
        std::getline(cin, county);
        counties.push_back(county);
    }


    int return_val = tier1(c0, c1, counties);

    if (return_val < 0) // print an error
        return 0;
}

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 the size member function of the vector.

int tier1(
    const std::string& c0,
    const std::string& c1,
    const std::vector<std::string>& counties)
{
    int votes[8];
    int i, j, N; // variables
    int k = 0;
    for (i = 0; i < counties.size(); i++)
    {
        cout << "county " << counties[i] << "\n";
        for (j = 0; j<2; j++)
        {
            cout << "How many votes did the candidate " << j << " get?\n";
            N = 0;
            cin >> N;
            votes[k++] = N;;
        }
        //checking if it goes over 100 votes
        if (votes[k - 2] + votes[k - 1] > 100)
        {
            cout << "One of the counties has too many votes. Exiting!\n";
            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;
}