2

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
EdCase
  • 119
  • 2
  • 7
  • Do you use C or C++ compiler for this? If the C standard is bellow C99, you can't do `for(int i=0; i – mutantkeyboard Jun 09 '20 at 10:12
  • I use a C compiler I beleive, it is on an IDE specifically for CS50 students. – EdCase Jun 09 '20 at 10:17
  • I tried the 'gcc -std=c99 main.c' and it said 'error: expected identifier or ‘(’ before ‘[’ token candidate[i].name = argv[i + 1];' But then again, this is part of the code that was already supplied to us. – EdCase Jun 09 '20 at 10:23
  • If you only want help for that compilation error, removing the unrelated parts of your code would be a good start to create a minimum example. – Gerhardh Jun 09 '20 at 10:23
  • The error message is not really telling much. You have a typo: There simply is no variable `cancidate` in your code and the compiler seems to take that line as a declaration of a function returning type `candidate` which also fails due to missing brackets. – Gerhardh Jun 09 '20 at 10:25
  • @Gerhardh Thank you sir, and noted about posting too much code. In my megre defense I didint know where the error actually was :) – EdCase Jun 09 '20 at 10:57
  • https://cs50.stackexchange.com/ – M.M Jun 23 '20 at 20:34

1 Answers1

2

Ok, this should fix it.

You're using struct name candidate:

typedef struct
{
    string name;
    int votes;
} candidate;

// Array of candidates
candidate candidates[MAX];

But in the for loop you iterate over candidate instead of candidates:

So change this:

 for (int i = 0; i < candidate_count; i++)
    {
        candidate[i].name = argv[i + 1];
        candidate[i].votes = 0;
    }

to this:

 for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
    }
mutantkeyboard
  • 1,614
  • 1
  • 16
  • 44
  • 2
    That has worked a treat, Thanks for your help. (It has thrown up some other errors, but isn't that the nature of code? – EdCase Jun 09 '20 at 10:56