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);
}
struct candidates electionCandidates[7];,void Initialize(struct candidates EC[]);,Initialize(electionCandidates);electionCandidatesdeclaration 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 offgets()to detect early EOF. Your calling syntax with the cast offpto(FILE *)fpis superfluous and indicates a lack of confidence. Passing&electionCandidatespasses a pointer to an array (struct candidates (*)[]) rather than just a pointer as you would if you dropped the&.