0

I am trying to have the program output the winner in a candidate race when trying to go through the array to get the highest count instead of the highest it gets the next highest from zero, and i feel like i have tried everything. I keep trying to change things around in the find winner function but nothing seems to be working I dont see what i am doing wrong please help.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

//User inputs data for candidates and votes.
//Output Candidates, votes, and percentage.
//Names that will be used are johnson, miller, duffy, robinson, ashtony
//count of votes to use = 5000, 4000, 6000, 2500, 1800, total 19300
//Percentages that will be used for candidates= 25.91, 20.73, 31.09, 12.95, 9.33

int findWinner(int votes[]);
void Results(string candidates[], int votes[]);
double Percentage(int votes[], int vote);
int tester[5] = {};
const int NUMBER_OF_CANDIDATES = 5;

int main()
{

   string candidates[NUMBER_OF_CANDIDATES];
   int votes[NUMBER_OF_CANDIDATES];

   cout << "Enter 5 Candidates with their votes ex: DelBosque 7000: ";
   for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
      cin >> candidates[i] >> votes[i];

   }
   cout << "Candidate  Votes Received  % of Total Votes" << endl;
   Results(candidates, votes);

   cout << "The Winner of the Election is " << candidates[findWinner(votes)] <<  
      endl;

   return 0;
}

double Percentage(int votes[], int vote){
   int sumOfVotes = 0;

   for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
      sumOfVotes += votes[i];
   }

   double percent = static_cast<double>(vote) / sumOfVotes;
   double votePercent = 0;
   votePercent = percent * 100;
   std::cout << std:: fixed;
   std::cout << std:: setprecision(2);
   std::cout << votePercent;

   return 0;
};


void Results(string candidates[], int votes[]){

   for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
      cout << candidates[i] << setw(15) << votes[i] << setw(15);
      int percent = Percentage(votes, votes[i]);
      cout << percent << "%" << endl;
   };
};

// You are returning the number of votes. Shouldn't you be returning the 
// index referenced by the highest number of votes?

int findWinner(int votes[]){
   int index = 0;
   int winner = 0;    
   for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
      if (votes[i] > winner)               
         winner = votes[i];  
      index = i;  


   };
   return index;
};
3
  • 1
    index = i; needs to be moved into the if block. Commented Aug 20, 2018 at 18:15
  • 1
    std::max_element is the tool you seem to be looking for. Commented Aug 20, 2018 at 18:18
  • Stack Overflow isn't a free debugging service, and you should show your attempts at debugging the code with a debugger or other simpler methods such as debug print statements. You can also test each part of the code separately to figure out exactly which part of the code is causing the problem, and make a minimal reproducible example. This won't be the only time you end up with a bug in your code, and learning to debug your programs will help you much more than having someone find the bug for you. Debugging your own code is an important skill in programming. See idownvotedbecau.se/nodebugging. Commented Aug 20, 2018 at 18:21

1 Answer 1

3

You need both the following lines under the if condition.

  winner = votes[i];  
  index = i;  

like:

  if (votes[i] > winner)               
  {
     winner = votes[i];  
     index = i;  
  }

What you have is equivalent to:

  if (votes[i] > winner)               
  {
     winner = votes[i];  
  }
  index = i;  

which is not correct.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks R Sahu I added the brackets and smooth sailing thanks a million.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.