/* CSE 303, Spring 2009, Marty Stepp This program reads a file of state-by-state 2008 presidential election polls, where each line contains a state code, percent votes for Obama/McCain, and number of electoral votes for that state: AL 34 54 9 AK 42 53 3 AZ 41 49 10 and outputs how many total electoral votes each candidate earned. */ #include #include #include int main(void) { int obama_total = 0; int mccain_total = 0; FILE* f = fopen("polls.txt", "r"); if (!f) { perror("Error while opening file"); return 1; } while (!feof(f)) { char state[4]; int obama, mccain, electoral_votes; if (fscanf(f, "%s %d %d %d", state, &obama, &mccain, &electoral_votes) == EOF) { perror("Error while reading file"); return 1; } if (obama > mccain) { obama_total += electoral_votes; } else if (mccain > obama) { mccain_total += electoral_votes; } } printf("Obama = %d, McCain = %d\n", obama_total, mccain_total); return 0; }