0

My code works consistently, but I get these two errors which I check it. I would like to know how to replicate these errors on my own so that I can figure out how to fix them.

#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);

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
    if(voter_count != 0){
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf(" Invalid vote.\n");
        } 
        else
        {
            for(int j = 0; j < candidate_count; j++) 
            {
                if(strcmp(candidates[j].name,name)==0) //if the candidate equals the name that was voted
                {
                    candidates[j].votes++; //add a vote for that candidate
                }
            }
        }
    }

    // Display winner of election
    print_winner();
    } else{
        printf("Invalid vote.\n");
    }
}

// Update vote totals given a new vote
bool vote(string name)
{

        int ifname = 0;
        for (int i = 0; i < candidate_count;i++){
            if(strcmp(name,candidates[i].name)==0) //if name equals the candidate's name
            {
                ifname++; //confirm that it is a valid name
            }

        } 
        if(ifname){
            return true;
        }else{
            return false;
        }
}



// Print the winner (or winners) of the election
void print_winner(void)
{
    candidate most[candidate_count];
    int winnersamnt = 0;
    int mostVotes = 0;
    for(int i = 0; i < candidate_count; i++){
        most[i] = candidates[i];
    }
    for(int i = 0; i < candidate_count; i++) 
    {
        if(most[0].votes < most[i].votes)  //if votes for the postition that is earlier is less than the vote of others, switch them
        { 
            most[0].name = most[i].name;
            most[0].votes = most[i].votes;

        } 
        if(i == candidate_count-1){ //if it is the last run
            mostVotes = most[0].votes; //the most amount of votes that any candidate got
        }

    } 
    if(mostVotes > 0){ //if there were any votes
        for (int i = 0; i< candidate_count; i++){
            if(candidates[i].votes == mostVotes ){
                winnersamnt++; //counts amount of winners
            }
        }

        for (int i = 0; i < winnersamnt; i++){ //print each winner's name
            printf("%s\n", most[i].name);
        }
    }
    else
    {
        printf("No winner\n");
    }
    return;
}

This is what I get when I check it:

:) plurality.c exists

:) plurality compiles

:) vote returns true when given name of first candidate

:) vote returns true when given name of middle candidate

:) vote returns true when given name of last candidate

:) vote returns false when given name of invalid candidate

:( vote produces correct counts when all votes are zero

vote function did not correctly update vote totals

:( vote produces correct counts after some have already voted

vote function did not correctly update vote totals

:) vote leaves vote counts unchanged when voting for invalid candidate

:) print_winner identifies Alice as winner of election

:) print_winner identifies Bob as winner of election

:) print_winner identifies Charlie as winner of election

:) print_winner prints multiple winners in case of tie

:) print_winner prints all names when all candidates are tied

1

There is no data that will show you what check50 sees. That is because program violates this instruction from the spec:

You should not modify anything else in plurality.c other than the implementations of the vote and print_winner functions (and the inclusion of additional header files, if you’d like).

check50 uses the distro main function for testing.

| improve this answer | |
  • I wish I could help but I can't even get my code to compile lmao. Can you explain your print winner section because I think that is where I am struggling at the moment. Maybe if I can get mine to compile I can help figure out why your code isn't updating the vote totals? I have a question with my code in it on my profile if you would like to take a look. – ayeeitsalpha Apr 5 at 23:37

You must log in to answer this question.

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