0
void print_winner(void)
{
   // TODO
   int c = 1;
   int numwin = 1;
   candidate mv[MAX];

   mv[0].votes = -1;

   for(int i = 0; i < candidate_count; i++)
   {
       if(candidates[i].votes > mv[0].votes)
       {
           mv[0] = candidates[i];
       }
       else if (mv[0].votes == candidates[i].votes)
       {
           mv[c] = candidates[i];
           c++;
       }
   }
   numwin = c;

   if (c == 1)
   {
       printf("%s\n", mv[0].name);
   }
   else if (c > 1)
   {
       for (c = 0; c < numwin; c++)
       {
           printf("%s ", mv[c].name);
       }
   }

   printf("\n");
   return;}

For some reason, this code gives the correct outputs yet the check50 says

:) 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 produces correct counts after some have already voted
:) 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 function did not print both winners of election
:( print_winner prints all names when all candidates are tied
   print_winner function did not print all three winners of election

even when I personally test it and am able to get these outputs to occur. A thing that might be of significance was that I was receiving segmentation faults while trying to work through previous versions of my code. However, that isn't happening anymore so I am unsure what is the problem.

EDIT: here is the entirety of the 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;
}
candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
void csearch(string name);
bool search(string name);
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");
            i = i - 1;
        }
    }

    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string name)
{
    // TODO
    if (search(name))
    {
        csearch(name);
        return true;
    }
    else
    {
        return false;
    }
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    // TODO
    int c = 1;
    int numwin = 1;
    candidate mv[MAX];

    mv[0].votes = -1;

    for(int i = 0; i < candidate_count; i++)
    {
        if(candidates[i].votes > mv[0].votes)
        {
            mv[0] = candidates[i];
        }
        else if (mv[0].votes == candidates[i].votes)
        {
            mv[c] = candidates[i];
            c++;
        }
    }
    numwin = c;

    if (c == 1)
    {
        printf("%s\n", mv[0].name);
    }
    else if (c > 1)
    {
        for (c = 0; c < numwin; c++)
        {
            printf("%s ", mv[c].name);
        }
    }

    printf("\n");
    return;
}

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

    return false;
}

void csearch(string name)
{
    for(int i = 0; i < candidate_count; i++)
    {
        if(strcmp(name, candidates[i].name) == 0)
        {
            candidates[i].votes++;
        }
    }
    return;
}
  • can you post the code that can compile? what are the steps to cause segfault? – stensal Jun 13 at 3:57
  • posted the rest of the code – user28696 Jun 13 at 5:08
0

I cannot reproduce segfault. But your code produced a wrong result for the following input.

$ ./a.out  Alice Bob Joe
Number of voters: 10
Vote: Alice
Vote: Alice
Vote: Alice
Vote: Bob
Vote: Bob
Vote: Bob
Vote: Joe
Vote: Joe
Vote: Joe
Vote: Joe
Joe Bob
| improve this answer | |
0

for (c = 0; c < numwin; c++) { printf("%s ", mv[c].name); }

Will only print once.

| improve this answer | |

You must log in to answer this question.

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