0
#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 and Max_votes
int candidate_count;
int max_votes = 0;

// 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");
        }
    }

    // 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 ++;

            if (candidates[i].votes >= max_votes)
            {
                max_votes = candidates[i].votes;
            }
            return true;
        
        }
    }

    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (max_votes == candidates[i].votes)
        {
            printf("%s\n", candidates[i].name);
        }
    }

    return;
}


0

Where is max_votes declared ?

I saw that you have put max_votes inside your vote function but I think you haven't initially declared it

Remember, Print winner should only print the name of the candidates who have max number of votes in the election

And how you can do that,

Well, You should loop through all the candidates by comparing the previous vote with current vote and check

if (max <= candidates[i].votes)

Then update the vote count of that candidate

Now,

Again use another loop to check rather

if (max == candidates[i].votes)

If yes, then print the name of the candidate(s)

If I helped you be sure to tick the answer to keep other smiling too 😃

9
  • Hey, I declare max_votes variable just below the candidates_count variable. I also did the same as you described just the variable being max_votes instead of max. Actually, when I check all the test cases they do work but check50 somehow doesn't. Thanks for helping :) – Vidhi Shah Jul 19 '20 at 16:51
  • Is your problem solved. – S.Shehroz Ali Jul 19 '20 at 17:37
  • No it doesn't even though I had already done what you recommended in my code. – Vidhi Shah Jul 19 '20 at 17:43
  • Please update the code if you have written whatever I have said so I can recheck it – S.Shehroz Ali Jul 19 '20 at 17:45
  • I have added another answer implement it to check again – S.Shehroz Ali Jul 19 '20 at 17:54
0

Why are you declaring !strcmp

This means that if name and candidates[i].name doesn't match then update the vote count of the candidate.

!

This is a bool operator which means false so instead you should do

strcmp() == 0
// Then do something

== 0 

Means if both name matches i.e. true then excute the if statement

0

Basically your print_winner is something like this

void print_winner(void)

This function is not passing any arguments nor it is accepting any return value

So if you use return inside your function so basically it will return NULL because print_winner is actually void

2
  • Not working still – Vidhi Shah Jul 19 '20 at 19:11
  • Put return true inside nested if in vote function , also remove spacing between candidates[i].vote and ++ – S.Shehroz Ali Jul 19 '20 at 19:16

You must log in to answer this question.

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