0
#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;
}
candidate;

// Array of candidates
// candidate is the data type, while candidates is the label
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes (These are just declaring functions)
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;
    }

    // this is the part that collects and iterates over the actual votes
    // not the part that iterates over the candidates
    int voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        // don't get rid of the string in string name for some reason
        string name = get_string("Vote: ");

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

    // Display winner of election
    print_winner();
}

// don't get rid of the string in string name for some reason
// Update vote totals given a new vote
// will take in the name of one of the candidates, (e.g "Bob")
bool vote(string name)
{
    // TODO
    // still not sure why string name is declared every time
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes++;
            return true;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    int a = 0;
    for (int b = 0; b < candidate_count;)
    {
        if (candidates[a].votes < candidates[b].votes)
        {
            a = b;
            b++;
        }
        else if (candidates[a].votes > candidates[b].votes)
        {
            b++;
        }
    }

    for (int c = 0; c < candidate_count; c++)
    {
        if (candidates[a].votes == candidates[c].votes)
        {
            printf("%s", candidates[c].name);
        }
    }

    printf("%s", candidates[a].name);
    return;
}

I think the logic of the code checks out. The idea of the return vote function is to iterate over each of the politician's vote count and record which one of the politicians has the most amount of votes, "i". If there are multiple politicians that have the same number of votes as "i" then it will iterate through all of them as well and print their names too! The code compiles, but when run does not give any names.

1 Answer 1

1

yo"re almost there. You made a small mistake in your implementation of your function print_winner() . If you don't see it I'd suggest the use of debug50 to check the value of your variables at every step of your program.

1
  • yeah i realized there was an issue with incrementing. Thanks!
    – michael
    Commented Apr 12, 2020 at 20:19

You must log in to answer this question.

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