/*////////////////// /// Vote Counter /// //////////////////*/ #include #include enum candidate { K=0, /* 'K'erry */ B=1, /* 'B'ush */ N=2, /* 'N'ader */ O=3 /* 'O'ther */ }; int main () { int Input; unsigned long total=0; /* Initialize the total vote count. */ unsigned long Tally[4]={0}; /* Initialize the array to keep running totals. */ while ((Input=getchar())!=EOF) { /* While the votes are coming in... */ unsigned char Vote=Input; if (!isspace(Vote)) /* If it is a valid vote. */ { switch(Vote) /* Determine who the vote as for. */ { case 'K': Tally[K] += 1; break; /* Kerry */ case 'B': Tally[B] += 1; break; /* Bush */ case 'N': Tally[N] += 1; break; /* Nader */ default : Tally[0] += 1; break; /* 0ther */ } /* End 'switch' statement. */ total += 1; /* Add one to the total number of votes. */ } /* End 'if' statement. */ } /* End 'while' loop. */ /* Check that the totals are consistent. */ if(total != Tally[K] + Tally[B] + Tally[N] + Tally[O]) printf("Error -- Voting totals are NOT consistent!\n"); /* Report voting fraud. */ else /* Looks good! */ { printf("Kerry: %d\n",Tally[K]); /* Report Kerry votes. */ printf("Bush: %d\n",Tally[B]); /* Report Bush votes. */ printf("Nader: %d\n",Tally[N]); /* Report Nader votes. */ printf("Other: %d\n",Tally[O]); /* Report other votes */ } return 0; /* Exit without errors. */ }