0

I have the following struct

struct candidates
    {
     char name[20];
     int votes;
    };
struct candidates electionCandidates[];

I need to read in from a file and update the names electionCandidates[0] through 7.

I can do so with the following

for (i = 0; i < 7; i++)
    {
        fgets(electionCandidates[i].name, 20, (FILE*)fp);
    }

But I need to do this in a function.

I have tried

void Initialize(struct candidates* EC[]);

Initialize(&electionCandidates);

void Initialize(struct candidates* EC[])
{
    int i;

    FILE *fp;
    fp = fopen("elections.txt", "r");

    for (i = 0; i < 7; i++)
    {
        fgets(EC[i].name, 20, (FILE*)fp);
    }

    fclose(fp);
}

Keeps saying it doesn't see name. Or the whole thing could be wrong. I am not sure.

Any help would be appreciated.

2
  • 1
    struct candidates electionCandidates[7];, void Initialize(struct candidates EC[]);, Initialize(electionCandidates); Commented Sep 4, 2014 at 19:00
  • The electionCandidates declaration at file scope with empty square brackets is dubious; somewhere along the line you need to specify its size. You should check that the file was successfully opened. You should always check the result of fgets() to detect early EOF. Your calling syntax with the cast of fp to (FILE *)fp is superfluous and indicates a lack of confidence. Passing &electionCandidates passes a pointer to an array (struct candidates (*)[]) rather than just a pointer as you would if you dropped the &. Commented Sep 4, 2014 at 19:01

3 Answers 3

1

You need to look at your Initialize() function signature, and turn on your compiler warnings (or pay attention to them if they're already on). You are declaring Initialize() to take an array of pointers to struct candidates, which is not what you are passing - an array of struct candidates. The array decays to a pointer, so struct candidates *EC is what your argument should look like (or, alternately, struct candidates EC[], which is equivalent in this case), not struct candidates *EC[]. And then call your function without the &...

Sign up to request clarification or add additional context in comments.

Comments

0

Given

struct candidates
    {
     char name[20];
     int votes;
    };
struct candidates electionCandidates[];

You want

void Initialize(struct candidates EC[])
{
    /* ... */
}

Initialize(electionCandidates);

When passed as a function argument, the array of structs decays to a pointer to its first element.

2 Comments

That is working :D I am getting the names loaded now. Thanks everyone
Be CAREFUL! Read again the comments to your question from BLUEPIXY and Johathan Lefler. Specifically, the dubious nature of declaring electionCandidates[];
0

I got it loading the names and everything but I guess I need to point to SPOIL in this? It is not reading or doing the votes/spoiled votes, giving 0 for both.

#include <stdio.h>

struct candidates
{
 char name[20];
 int votes;
};
struct candidates electionCandidates[7];

void Initialize(struct candidates EC[]);
void Processvotes(struct candidates EC[], int BadVote);
void printResults(struct candidates EC[], int BadVote);

int main()

{

    int i, SPOIL = 0;

    Initialize(electionCandidates);
    Processvotes(electionCandidates, SPOIL);
    printResults(electionCandidates, SPOIL);

}

void Initialize(struct candidates EC[])
{
    int i;

    FILE *fp;
    fp = fopen("elections.txt", "r");

    for (i = 0; i < 7; i++)

    {
        fgets(EC[i].name, 20, (FILE*)fp);
    }

    fclose(fp);
}


void Processvotes(struct candidates EC[], int BadVote)
{
        int TVOTE, i;

        FILE *fp;
        fp = fopen("elections.txt", "r");

        fscanf(fp, "%d", &TVOTE);

        for (i = 0; i < 365; i++)
        {
            if (TVOTE == 1)
                EC[0].votes++;
            if (TVOTE == 2)
                EC[1].votes++;
            if (TVOTE == 3)
                EC[2].votes++;
            if (TVOTE == 4)
                EC[3].votes++;
            if (TVOTE == 5)
                EC[4].votes++;
            if (TVOTE == 6)
                EC[5].votes++;
            if (TVOTE == 7)
                EC[6].votes++;

            if (TVOTE < 1 || TVOTE > 7)
                BadVote++;
        }

        fclose(fp);
}

void printResults(struct candidates EC[], int BadVote)
{

    int i, Win = 0, WinSCORE = 0, Runner = 0, RunnerSCORE = 0;

    for (i = 0; i < 7; i++)
    {

        if (EC[i].votes > WinSCORE)
        {
            WinSCORE = EC[i].votes;
            Win = i;
        }

        if (EC[i].votes == WinSCORE)
        {
            RunnerSCORE = EC[i].votes;
            Runner = i;
        }
    }


    if (WinSCORE == RunnerSCORE)
    {
        printf("There was a tie between %s and %s who both got a total of %d votes each. There were %d spoiled votes\n", EC[Win].name, EC[Runner].name, WinSCORE, BadVote);
    }

    else
        printf("%s won the election with a total of %d votes. There was a total of %d spoiled votes.\n", EC[Win].name, WinSCORE, BadVote);


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.