Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

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

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--;
        }
    }

    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string name)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(name, candidates[i].name) == 0)
        {
            candidates[i].votes++;
            return true;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
void print_winner()
{
    int maxvotes = candidates[0].votes;
    for (int i = 0; 1 < candidate_count; i++)
    {
        if(candidates[i].votes > maxvotes)
        {
            maxvotes = candidates[i].votes;
        }
    }

    for(int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == maxvotes)
        {
            printf("%s\n",candidates[i].name);
        }
    }
    return;
}


What I have tried:

this is the first time its happening with me please tell me how to solve
Posted
Updated 27-Nov-22 3:36am

I cannot complier your code, there are a few of wrong code lines in it.
e.g.:
*file cs50.h isn't existed, (thank @Richard MacCutchan to tell me about this library)
*you should include <string> but <string.h> to use std::string,
*main function should be int main(int argc, char* argv[])
*you should include <cstring> to use strcmp
*get_int\get_string undefined

but, maybe the code block which caused crashed is:
for (int i = 0; 1 < candidate_count; i++)
   {
       if(candidates[i].votes > maxvotes)
       {
           maxvotes = candidates[i].votes;
       }
   }

your coded:
1 < candidate_count
but not
i < candidate_count

it will access an invalid address
 
Share this answer
 
v2
Comments
Richard MacCutchan 27-Nov-22 11:33am    
The cs50.h file is part of CS50 Manual Pages[^].
longjmp 27-Nov-22 21:31pm    
@Richard MacCutchan: Thanks for telling me about this useful library, I didn't know about it before (I compiled the code on cygwin and it didn't provide that library), it looks good.
CPallini 28-Nov-22 4:02am    
Good catch, my 5.
A segmentation fault means that you are accessing memory that isn't yours: usually that's the result of a pointer operation on a bad or null pointer, but it's also possible to get it with an array if you try to use an index that doesn't exist.

We can't fix that for you: we have no idea where in that code the problem lies, we can't run your code under the same circumstances you can (for example, we have no idea what parameters you passed to your app when you ran it), we can't see the error message.

At a guess, I'd suggest that it was probably the argument list - Are your really passing the number of arguments you think you are? But a guess is all that can be, and it could be miles wide of the mark.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900