This program is for counting the plurality for a list of candidates, Check Description for more info, The program works like this:

  1. Enter the candidate's name as an argument when running the program for example:
./plurality John Jack Tom Timara
  1. The program will ask you to enter the Number of voters
  2. The program will ask you for your vote, It should be one of the candidates' names (Case Sensitive).
  3. After you enter the candidate's name the program will ask you again for your vote depend on your Number of voters.
  4. The program will output the winning candidate. (Could be more than one candidate if they are equal)

What is my problem?

Let's say when I enter Number of voters: 6 and vote 3 times for John and 3 times for Jack the output will be:

The winner is: John, 

But if I vote 3 times for John and 3 times for Tom or Timara the output will be:

The winner is: John, Tom

The program doesn't work correctly if the first candidate and the second have an equal amount of votes and I don't know why! Here is My code:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
    string name;
    int votes;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
    // Check for invalid usage
    if (argc < 2)
        printf("Usage: plurality [candidate ...]\n");
        return 1;

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX)
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    for (int i = 0; i < candidate_count; i++)
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;

    int voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
            printf("Invalid vote.\n");

    // Display winner of election

// Update vote totals given a new vote
bool vote(string name)
[snip]    return exist;

// Print the winner (or winners) of the election
void print_winner(void)
  • I'm having a lot of problems doing plurality too. Can you explain the bool exist part?
    – Betty Van
    Commented Oct 24, 2019 at 20:21
  • First, when the function vote runs I'll assume that the candidate that I'm looking for does not exist so I create a new bool variable called exist and set its default value to false. Next, I create a loop to look at each candidate in candidates[i].name, to do that you need to use a function called strcmp(), you can learn more about it from here. The strcmp() function compares two strings if they are equal the return value will be 0, (continue in the next comment).
    – Budi Salah
    Commented Oct 25, 2019 at 16:30
  • So in my condition if (strcmp(name, candidates[i].name) == 0) if its true I'll update the candidate[i].votes and add 1 to it, then I'll update the exist bool to equal true and then I'll out from this loop by using break so I don't waste more time searching for the candidate. then at the end of the function I'll return the current exist value.
    – Budi Salah
    Commented Oct 25, 2019 at 16:30
  • I appreciate the help. I dont think there was a lot of documentation on this problem set. This is the best I could find. The lecture doesn't quite explain this, they get into the basics of typedef struct. For example intializing a new arrary candidate candidateholder; is greek to me. I would like to see another solution to the same problem highly commented or runoff or tide, subsequent problems in the series, but I super appreciate this post alot.
    – msippel415
    Commented Dec 17, 2019 at 14:13

1 Answer 1


You should rethink your logic for the print_winners function.

Here's the pseudocode I'd use.

  • Declare a maxvote int set to 0.
  • Iterate over the list of candidates and check the number of votes. If that number is greater than the maxvote, update the maxvote to that number. Once the loop finishes, you now have maxvote.

  • Next, again iterate over the list of candidates and print (as you go) each candidate who has that maxvote number of votes.

There's no need as you've done to create a new string and then try to manipulate that.

As the spec shows, the winner(s) should simply be printed at the end, one per line.

$ ./plurality Alice Bob Charlie
Number of voters: 5
Vote: Alice
Vote: Charlie
Vote: Bob
Vote: Bob
Vote: Alice
  • thank you for your answer, Quick question, I always have trouble in logic and writing pseudocode, So how I can improve them?
    – Budi Salah
    Commented Oct 25, 2019 at 16:38
  • as far as incrementing the maxvote int would maxvote++ only increment it by one? If so how do you get it to increment to the specific number that is greater than the current maxvote? Commented Apr 5, 2020 at 23:03
  • @ayeeitsalpha you don't increment it, you set it to the value.
    – curiouskiwi
    Commented Apr 6, 2020 at 3:12
  • Curiouskiwi Many thanks for your pseudocode logic. It is much simpler and easier to implement than what my logic was, and that's why I was struggling to code it in like @budi-salah. But now my programme works! Yeehah! Commented May 4, 2020 at 12:48
  • Tnx.. your pseudocode was very helpful. I was thinking to difficult trying to push winners in an Array and then print Array of winners.
    – Drallas
    Commented Jul 28, 2021 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .