I have a homework assignment that I am working on that deals with an array of structs. Basically I have to read data from a text file (elections.txt) that looks like this and then write these names into an array of structs for names:
Robert Bloom
John Brown
Michelle Dawn
Michael Hall
Sean O’Rielly
Arthur Smith
Carl White
The code I have written so far looks like this:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
FILE * data;
typedef struct
{
int votes;
char name[20];
}candidates;
void initialize( candidates *electionCandidates, FILE *data )
{
int i;
for( i=0; i<7; i++ );
{
fscanf( data, "%[^\n]%*c", electionCandidates[i].name );
printf( "%s\n", electionCandidates[i].name );
}
}
int processVotes( int p1, int p2 )
{
}
void printResults( int p1, int p2 )
{
}
int main() {
data = fopen( "elections.txt","r" );
candidates electionCandidates[7];
initialize( electionCandidates, data );
fclose( data );
return 0;
}
My problem is that when I run this program, what is printed on the screen is only the first name instead of all the names. I don't understand why this could be because I am looping through 7 positions of my struct array in the function. Can anyone point me in the right direction?
for( i=0; i<7; i++ );: remove;;? the semi-colon after the')'should not be there.