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 candidates[MAX];

// Number of candidates
int candidate_count;

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

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: ");
        jason(name);
        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }

    // Display winner of election
    print_winner();
}

bool vote(string name)
{
    int t = candidate_count;
    for(int i = 0;i<t;i++)
    {
        if (strcmp(candidates[i].name,name)==0)
        {
            return true;
        }
    }
    // TODO
    return false;
}

void jason(string name)
{
    for (int i=0;i<candidate_count;i++)
    {
        if (strcmp(candidates[i].name,name)==0)
        {
            candidates[i].votes++;
        }
    }
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    int max = 0;
    for(int j=0;j<candidate_count;j++)
    {
        if(candidates[j].votes>max)
        {
            max = candidates[j].votes;
        }
    }
    for (int k=0;k<candidate_count;k++)
    {
        if (max == candidates[k].votes)
        {
            printf("%s\n",candidates[k].name);
        }
    }
    return;
}

How do i fix the issue?

0
void jason(string name)
{
    for (int i=0;i<candidate_count;i++)
    {
        if (strcmp(candidates[i].name,name)==0)
        {
            candidates[i].votes++;
        }
    }
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    int max = 0;
    for(int j=0;j<candidate_count;j++)
    {
        if(candidates[j].votes>max)
        {
            max = candidates[j].votes;
        }
    }
    for (int k=0;k<candidate_count;k++)
    {
        if (max == candidates[k].votes)
        {
            printf("%s\n",candidates[k].name);
        }
    }
    return;
}

this is the part that got jumbled while posting how do i fix the :( vote produces correct counts when all votes are zero and :( vote produces correct counts after some have already voted

what do these even mean?

also,in the main pro provided,it does not even store the count,so i had to modify it to make sure it stored it.

1
  • Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review – Cliff B Feb 2 at 7:40

You must log in to answer this question.

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