/* voteCounter.cpp   Demonstrate reading data from a file
   P. Conrad, 9/23/04, CISC105, Fall 2004 */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  FILE *infile;
  char state[3];  /* 2 for the state, one to terminate the string */
  char candidate;
  int electoralVotes;

  int result; /* for result of scanf */

  int kerry=0, bush=0, tie=0; /* declaring variables, and initializing to zero */

  infile = fopen("electoralVotes.dat","r");

  if (infile==NULL)
    {
      fprintf(stderr,"File could not be opened.  Sorry about that.\n");
      exit(-1);
    }
  
  result = fscanf(infile,"%s\t%c\t%d",&state, &candidate, &electoralVotes);
  while (result != EOF)
    {
      if (candidate=='B')
	{
	  /* then add electoralVotes to total for bush */
	  bush  = bush + electoralVotes;
	}
      else if (candidate == 'K')
	{
	  kerry = kerry + electoralVotes;
	}
      else if (candidate == 'T')
	{
	  tie = tie + electoralVotes;
	}
      else
	{
	  fprintf(stderr,"Unexpected data in file: %c\n",
		  candidate);
	  exit(-1);
	}
      
      /* read the next line of data */
      result = fscanf(infile,"%s\t%c\t%d",&state, &candidate, &electoralVotes);
    }

  
  printf("%d %d %d\n", bush, kerry, tie);
  printf("All done with all lines in the file\n");
  return 0;
}

