I'm doing PSET3 through CS50 and I am having an issue compiling the code. It keeps on coming up with the error
plurality.c:44:21: error: expected identifier or '('
candidate[i].name = argv[i + 1];
This is strange, as this is in the section of code that was given before we started the problem.
The full code I have is here...
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.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++)
{
candidate[i].name = argv[i + 1];
candidate[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)
{
// TODO
// Local boolean variable
bool valid_vote = false;
// Iterating over number of candidates and comparing string "name" to get a boolean result.
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(name, candidate[i].name) == 0)
{
candidate[i].votes++;
valid_vote = true;
// Force a break in the program to exit with the correct result
break;
}
}
return valid_vote;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
// Initialising a variable for the most votes, and the candidate
int highest_votes = candidates[0].votes;
string win = candidates[0].name;
// Looping through results and finding the highest "votes" value
for (int i = 0; i < candidate_count; i++)
{
if (candidate[i].votes > highest_votes)
{
highest_votes = candidate[i].votes;
win = candidate[i].name;
}
}
// Comparing all "votes" values and printing winner(s)
for (int j = 0; j < candidate_count; j++)
{
if (candidate[j].name == win)
{
printf("%s %s", win, candidates[j].name);
}
printf("%s", win);
}
return 0;
}
To clarify, I dont want help on the problem, just the specific complilation error.
Thanks guys