Hi, I'm having issues with pset3 , Plurality and check50 . Hi, I cannot find any problem with my code. It seems to do everything that is supposed to do.
Here's the code:
// cs50 2020
//PSET 3 Plurality
// takes count of user defined number of
//voters in election and returns result
//
// Max number of candidates
// 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;
//checks if candidate numberis above MAX
if (candidate_count > MAX)
{
printf("Maximum number of candidates is %i\n", MAX);
return 2;
}
//assigns location in array
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++;
return true;
}
}
return false;
}
// find and store highest vote total
void print_winner(void)
{
int record = 0;
for (int i = 0; i < candidate_count; i++)
{
if ( candidates[i].votes > record)
{
record = candidates[i].votes;
}
}
//print winners
//printf("The winner is/are: \n");
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == record)
{
printf("%s \n", candidates[i].name);
}
}
return;
}
I get these errors from check50
:( print_winner identifies Alice as winner of election
print_winner function did not print winner of election
:( print_winner identifies Bob as winner of election
print_winner function did not print 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 function did not print both winners of election
:( print_winner prints all names when all candidates are tied
print_winner function did not print all three winners of election
Any help would be most appreciated
. ¦3<RELATIVISM
is a good, or bpa.st one or is also trusted
. ¦3<RELATIVISM
fuegopazzo (Gitter):
. ¦3<RELATIVISM
seach for that term
. ¦3<RELATIVISM
usually you change something and just ask for people to pull from your repositorie
. ¦3<RELATIVISM
I can be wrong though given I never submited it
. ¦3<RELATIVISM
there is also a package called submit50, which might be for that
. ¦3<RELATIVISM
Hopefully this helped cheers
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../x86_64-linux-gnu/crt1.o: In function _start':
(.text+0x20): undefined reference to
main'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
<builtin>: recipe for target 'cash' failed
make: * [cash] Error 1