Here's 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
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; candidate_count > i; i++)
{
if (strcmp(candidates[i].name, name) == 0)
{
candidates[i].votes++;
return true;
}
}
return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
// TODO
// Print candidate(s) with the most votes
// Loop through all the votes, compare which is higher, and the highest one gets printed out
int tieCount = 0;
for (int i = 0; candidate_count > i; i++)
{
for (int j = 0; candidate_count > j; j++)
{
// Tests for equality
int equalComp = j;
if(equalComp == i)
{
equalComp += 1;
}
if (candidates[i].votes == candidates[equalComp].votes) // Compares to the if numbers are equal
{
tieCount++;
printf("%s\n", candidates[j].name);
if (tieCount >= candidate_count)
{
return;
}
}
if (equalComp == candidate_count) // Breaks if the comparision number overshots the loop
{
return;
}
}
for (int j = 0; candidate_count > j; j++)
{
// Tests for greater than
int greaterComp = j;
if(greaterComp == i)
{
greaterComp += 1;
}
if (candidates[i].votes > candidates[greaterComp].votes) // Compares to the if numbers are greater than
{
printf("%s\n", candidates[i].name);
return;
}
if (greaterComp == candidate_count) // Breaks if the comparision number overshots the loop
{
return;
}
}
}
return;
}
And here's the check50 output:
:) 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 function did not print winner of election
:) print_winner prints multiple winners in case of tie
:) print_winner prints all names when all candidates are tied
I don't understand why it doesn't print Charlie as the winner, any advice? I've been stuck on this for about two days now, and i finally got it to work as what i understand the given specifications, and i dont really understand why it only seems to get stuck on Charlie.
Thanks so much for your help, much love :D