0

When I compile this code, it is able to do so with no errors. However when entering in "check50 cs50/problems/2020/x/plurality" into the terminal to see if I fulfilled all the requirements, the check50 says that the code has failed to compile. I checked the errors of why it has failed to compile and the errors look like it is for a different program. Thinking it was checking a different program, I submitted my code and got a 1/14 because it couldn't compile. What did I do wrong?

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

typedef struct
{
    string name;
    int votes;
}
    candidate;

candidate candidates[9];

int limits;

bool vote(string name) 
{
    for(int i = 1; i < limits; i++)
    {
        if(strcmp(name, candidates[i].name) == 0)
        {
            candidates[i].votes++;
            return true;
        }
        else
        {
            continue;
        }
    }
    return false;
}


void print_winner(void)
{
    string winner[10];
    int winnerVotes[10];
    winner[0] = candidates[1].name;
    winnerVotes[0] = candidates[1].votes;
    for(int i = 2; i < limits; i++)
    {
        if(winnerVotes[0] > candidates[i].votes)
        {
            continue;
        }
        else if(winnerVotes[0] == candidates[i].votes)
        {
            winnerVotes[1] = candidates[i].votes;
            winner[1] = candidates[i].name;
        }
        else
        {
            winner[0] = candidates[i].name;
            winnerVotes[0] = candidates[i].votes;
        }
    }

    if(winnerVotes[0] > winnerVotes[1])
    {
        printf("%s\n", winner[0]);
    }
    else if (winnerVotes[0] == winnerVotes[1])
    {
        printf("%s, %s\n", winner[0], winner[1]);
    }
    else
    {
        printf("%s\n", winner[1]);
    }

}


int main(int argc, string argv[])
{
    if(argc > 9 || argc < 3)
    {
        exit(1);
    }


    limits = argc;

    for(int i = 1; i < argc; i++)
    {
        candidates[i].name = argv[i]; // candidates start at 1 not 0
    }

    string voters = get_string("Number of voters: ");



    for(int i = 0; i < strlen(voters); i++)
    {
        if(voters[i] - '0' > 9 || voters[i] - '0' < 1)
        {
          exit(0); // make sure this works

        }
        else
        {
            continue;
        }
    }

    int votersInt = atoi(voters);

    string candVotes[votersInt];

    for(int i = 0; i < votersInt; i++)
    {
        candVotes[i] = get_string("Votes: ");
        if(vote(candVotes[i]))
        {
            continue;
        }
        else
        {
            printf("Invalid vote\n");
        }
    }

  print_winner();

}
0

Hmmm.... could it be because main() was changed when the instructions said not to??? Or that you didn't use the distro program that was given????

[edit] This is main from the distribution. Its massively different from your posted code. Where did you get yours? Are you sure that you're using the 2020 distribution?

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
    print_winner();
}
| improve this answer | |
  • I don't think I changed anything about main and I used the correct distro program. It's strange because the errors from the check50 seem to be coming from a different program, but I still got a 1/14 when submitting the code, so I don't really know what to do. Nevertheless, I really appreciate you trying to help me. Thanks! – Dylan Dang Jun 4 at 23:08

You must log in to answer this question.

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