I am sure this is going to be the first of a couple of questions about this assignment. I also understand there are probably many problems with the code I am about to show but I do have two general questions.

One, it seems that I am capable of pulling our structs(candidates) defined in Main() but for some reason I can't pull the int voter count. Am I doing something wrong or is there some larger programing rule at work like structures defined outside of main() but initialized inside of main() are fair game? Where as an int both declared and initialized inside of main cannot be passed through to a void function?

Second question. I feel like my some of my code might be working ok. I can't test it because I am having a couple other compiling errors. The one that I would like to fix is:

testp.c:88:9: error: expected expression candidate swap[MAX];

I have written my code such that I need a struct similar to candidates to hold the value of a candidate while I change the values of other candidates. I am having some trouble initializing this place holder. Any advice would be greatly appreciated.

I tried to comment each line to show my thinking.

#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;

// 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

// Update vote totals given a new vote
bool vote(string name)
    for(int i = 0; i<=candidate_count; i++)
        if (name == candidates[i].name)
            return true;
        }// TODO
    return false;

// Print the winner (or winners) of the election
void print_winner(void)

    int l = voter_count;        //number of total votes
    int i = 0;                  //counter

    for(i=0;i<l-1; i++)         //starts and re-initializes search as lines are read
        candidate swap[MAX];   //holds a string value to swap indexes
            for(int j = i +1; j < l-1; j++) //initializes each line.  J has a value of i + 1 to directly compare two adjacent indexes
                if(candidates[i].votes < candidates[j].votes)//change i to j and j to j+1
                swap = candidates[i].name;               //stores candidate [i] value
                candidates[i].name = candidates[j].name;  //replaces index [i] with index [j]
                candidates[j].name = swap;               //replaces index [j] with swap whch holds the value of index[i]

    printf ("%s \n", candidates[0].name);
    for (int c = 1; i <= candidate_count; c++)
        if (candidates[c].votes == candidates[0].votes)
            printf("%s, \n", candidates[c].name);

Thanks, Rob

  • you could use help50 to sort out the compilation errors first and then, proceed on to check you code. – Ayush Apr 18 at 8:26

You must log in to answer this question.

Browse other questions tagged .