0

So I have a few questions on Plurality check50 errors for this Pset:

My Code is as follows:

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.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);
void print_winner(void);

// Global Variables



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: ");

// collect all votes
for (int i = 0; i < voter_count; i++)
{

    string name = get_string("Vote: ");

    // Check for invalid vote

    // first declarte the vote as uncounted

    if (!vote(name))
    {

        printf("Invalid Vote.\n");
    }

}

// Display winner of election

for (int j = 0; j < candidate_count; j++)
{
    printf("candidate: %s\n votes:%d\n", candidates[j].name, candidates[j].votes);
}

print_winner();
}

// Update vote totals given a new  - Define the 'vote' function
bool vote(string name)
{

bool user_vote = false;

// if vote == struct name[i], votes++ (count the vote)
for (int i = 0; i <= candidate_count; i++)
{

    // Check for a valid vote

    if (strcmp(name, candidates[i].name) == 0)
    {
        candidates[i].votes++;
        user_vote = true;
        break;
    }


}

return user_vote;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
// TODO (sort the struct and take the highest figure to printf)

int most_votes = 0;

for (int i = 0; i < candidate_count; i++)
{

    if (candidates[i].votes > most_votes)
    {
        most_votes = candidates[i].votes;
    }

}

for (int i = 0; i < candidate_count; i++)
{

    if (candidates[i].votes == most_votes)
    {
        printf("%s\n", candidates[i].name);
    }

}

return;
}

The error messages i have with "check50" are the following:

 :( vote returns false when given name of invalid candidate
vote function did not return false
 :( vote leaves vote counts unchanged when voting for invalid candidate
vote function modified vote totals incorrectly

The questions I have are the following:

  • Why is check50 stating that an invalid vote does not return a false vote?

Within main there is a checking process for invalid votes ( "if (!vote(name)" ) to my knowledge this is the only instence which I call the function vote into the programme. And within the vote function the initial line of the code declares the user vote false (bool user_vote = false). The code then should enter the for loop but should fail to meet the "if" condition ( if (strcmp(name, candidates[i].name) == 0) ) and therefore fall past this to the end of the function to return the user_vote (return user_vote) which is unchanged since its declaration (false). So i dont understand why it states the vote is not returned as false.

  • My next question is why does check50 state that the voter counts are modified incorrectly?

Within the pset explanation they did not ask us to reprompt the user for a valid vote for a listed candidate the vote is simply noted as invalid but still counts toward the vote count (total number of votes casted). So in my view here the voter count is uneffected, why do i get that error in check50?

Thanks for your help in advance :)

11
  • take out user_vote and just write return false Jul 23, 2020 at 15:29
  • Unfortunately this did nt solve the problem.
    – Pot Noodle
    Jul 23, 2020 at 15:49
  • What this did was change valid votes to invalid votes as the function returns false regardless of the input.
    – Pot Noodle
    Jul 23, 2020 at 15:56
  • oh sorry :( ... Jul 23, 2020 at 16:29
  • no problem mate :) thanks for the suggestion.
    – Pot Noodle
    Jul 23, 2020 at 16:54

1 Answer 1

1

So it turns out the issue was with this line of code within the "vote" function:

for (int i = 0; i <= candidate_count; i++)

it needed to be:

for (int i = 0; i < candidate_count; i++)

the extra iteration was causing a segmentation error as it was essentially attempting to loop over a piece of data that wasnt there.

You must log in to answer this question.

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