简体   繁体   中英

the arrays won't add up the votes correctly?

I am writing a program that will tally votes from each counties up to 4 for two candidates. Basically it will ask you to add the votes in each counties for each candidate. Then it will tally the votes and display the winner. This mostly works but It won't add up the votes and declare the winner correctly? I suspect it is something wrong with the loop at line 37 to line 40.

#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[7];
 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 << "County has too many votes. Exiting!\n"; // Print an error
    exit(1);
   }

 }

 int candidate0Votes = 0; //resetting 
 int candidate1Votes = 0;
 for (i = 0; i < 7; i++)
 {
 cout << votes[i+1] << "\n";
 cout << votes[i+1] << "\n";
 candidate0Votes += votes[i+1];
 candidate1Votes += votes[i+1];
 }
 if (candidate0Votes > candidate1Votes){
 cout << "The winner of the election is candidate 0.\n";
 }
 else
 {
 cout << "The winner of the election is candidate 1.\n";
 }
 cout << "Here is the voting results:\n";
 cout << "candidate 0 got ";
 cout << candidate0Votes;
 cout << " votes\n";
 cout << "candidate 1 got ";
 cout << candidate1Votes;
 cout << " votes\n";

 return 0;

}

Let me know if there is any other revision that I should do! Thanks!

Your votes array is short.

It seems you have 4x2 entries, but the array is only 7 elements.

Here's a version that I could live with:

Live On Coliru

#include <iostream>
#include <numeric>
using namespace std;

int tier1();

int main(void) {
    return tier1();
}

static int getnumber(std::string prompt) {
    if (!std::cin)
        return 0;

    int i;
    std::cout << prompt;
    std::cin >> i;

    while(!std::cin) {
        if (std::cin.eof()) {
            exit(1); 
        }
        std::cin.clear();
        std::cin.ignore(1<<30, '\n');
        std::cout << prompt;
        std::cin >> i;
    }

    return i;
}

#include <map>
#include <algorithm>

int tier1() {
    using namespace std;
    using county = int;

    static constexpr int number_of_counties   = 4;
    static constexpr int number_of_candidates = 2;

    map<county, size_t[number_of_candidates]> votes;

    for (int cty = 0; cty < number_of_counties; cty++) {
        for (int cand = 0; cand < number_of_candidates; cand++) {
            votes[cty][cand] = getnumber("How many votes did the candidate " + to_string(cand) + " get in county " + to_string(cty) + "? ");
        }
        if (std::accumulate(begin(votes[cty]), end(votes[cty]), 0u) > 100)
            cout << "County has too many votes. Exiting!\n"; // Print an error
    }

    size_t totals[number_of_candidates] = { 0 };

    for(auto& cty: votes) {
        for (auto in = begin(cty.second), accum = begin(totals); accum < end(totals);) {
            *accum++ += *in++;
        }
    }

    auto winner = max_element(begin(totals), end(totals));

    cout << "The winner of the election is candidate " << distance(totals, winner) << "\n";

    cout << "Here is the voting results:\n";
    for (int cand = 0; cand < number_of_candidates; cand++) {
        cout << "candidate " << cand << " got " << totals[cand] << " votes\n";
    }

    return 0;
}

In addition to sehe's observation, I also see you have the following:

for (i = 0; i < 7; i++)
{
  cout << votes[i+1] << "\n";
  cout << votes[i+1] << "\n";
  candidate0Votes += votes[i+1];
  candidate1Votes += votes[i+1];
}

This should probably be (after you make your array size 8):

// Loop through all countries
for (i = 0; i < 8; i = i + 2)
{
  // Print out the candidate's votes per country
  cout << votes[i] << "\n";
  cout << votes[i+1] << "\n";
  // Total up each candidate's votes
  candidate0Votes += votes[i];
  candidate1Votes += votes[i+1];
}

I think this is what you want. The array size was changed to 8, and the for loop in line 37 to 40 was also modified.

#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 << "County has too many votes. Exiting!\n"; // Print an error
            exit(1);
        }

    }

    int candidate0Votes = 0; //resetting
    int candidate1Votes = 0;
    for (i = 0; i < 8; i+=2)
    {
        cout << votes[i] << "\n";
        cout << votes[i+1] << "\n";
        candidate0Votes += votes[i];
        candidate1Votes += votes[i+1];
    }
    if (candidate0Votes > candidate1Votes){
        cout << "The winner of the election is candidate 0.\n";
    }
    else
    {
        cout << "The winner of the election is candidate 1.\n";
    }
    cout << "Here is the voting results:\n";
    cout << "candidate 0 got ";
    cout << candidate0Votes;
    cout << " votes\n";
    cout << "candidate 1 got ";
    cout << candidate1Votes;
    cout << " votes\n";

    return 0;

}

Here is an improved suggestion which structures the code in a way that makes more sense (altough my original suggestion is probably easier):

You could use a two dimensional array as follows:

int votes[2][4]; //Store the votes in the format [candidate 0 or 1][country 0 to 3]

The loop in question would then become:

// Loop through all countries
for (i = 0; i < 4; i++)
{
    // Print out the candidate's votes per country
    cout << votes[0][i] << "\n";
    cout << votes[1][i] << "\n";

    // Total up each candidate's votes
    candidate0Votes += votes[0][i];
    candidate1Votes += votes[1][i];
}

The rest of the code would need to be modified to adapt to that structure as well. Since this could be a bit confusing, I left it as a separate answer. It is probably a bit better way to structure the program, but my original answer should fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM