0

When I compile my code it works, but when I try to run it, it says segmentation fault. I believe the segmentation fault is on the line that says printf("Winner is %s with %i votes\n", winner, most); I provided my code below, and help on how to fix it would be nice. Thanks.

#include <stdio.h>
#include <cs50.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: [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();
}    
    

//update vote totals given a new vote
bool vote(string name)
{
    bool got = false;
    for(int y = 0; y < candidate_count; y++)
    {
        if(strcmp(name, candidates[y].name) == 0)
        {
           candidates[y].votes += 1;

            got = true;

            break;
        }
    }
    return got;
}

void print_winner(void)
{
    int most = 0;
    string winner;

    for(int j = 0; j < candidate_count; j++)
    {
        if(candidates[j].votes >= most)
        {
            most = candidates[j].votes;
        }
    }

    for(int x = 0; x < candidate_count; x++)
    {
        if(candidates[j].name >= winner)
        {
            winner = candidates[j].name;
        }
    }
    printf("Winner is %s with %i votes\n", winner, most);
    return;
}
0

It's clearly a problem with winner. At runtime, the system doesn't like what's in winner.

You also have a basic problem with this code.

 for(int x = 0; x < candidate_count; x++)
    {
        if(candidates[j].name >= winner)
        {
            winner = candidates[j].name;
        }
    }
    printf("Winner is %s with %i votes\n", winner, most);
    return;

It's supposed to be checking for any candidate with the same number of votes as stored in most. How can it determine the number of votes by looking at the name of the candidate, not to mention comparing it to the contents of winner, which hasn't been initialized? And then, it's trying to use j instead of x as the index.

I suggest changing this code to look at the number of votes and when it finds a winning candidate, print the name by accessing it directly instead of using winner as an intermediate variable.

BTW, what happens if there's a tie and it's supposed to print more than one name?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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