logo CodeStepByStep logo

election

Language/Type: C++ streams file input map STL
Related Links:

Write a function named election that conducts a mock US Presidential election between two candidates, computing who won each state and who won the overall election. Your function accepts four string parameters: the names of the two candidates, and the names of two input files containing information about each state and about the votes cast.

Background: The United States uses a Presidential election system called the electoral college. In this system each state has a certain number of electoral votes that it can cast for the President. People in each state vote, and the candidate who receives the most popular votes wins all of the electoral votes for that state. For example, if Nebraska has 5 electoral votes and Carl wins Nebraska's popular vote 347 to 289, Carl receives all 5 of Nebraska's electoral votes. The candidate who receives the most electoral votes from the states wins the election.

In this problem you will read data from two files. The first input file contains information about every state that will take part in the election. Each line of this file contains exactly two whitespace-separated tokens: the state's two-letter abbreviation, and the number of electoral votes that state has. See the example below at left. For example, a line of "NE 5" means that the state of Nebraska (NE) has 5 electoral votes.

The second input file contains a series of lines representing popular votes in various regions. Each such line contains exactly three whitespace-separated tokens: the state's two-letter abbreviation, the number of votes for the first candidate, and the number of votes for the second candidate. For example, a line of "NE 37 56" indicates that in part of Nebraska (NE), there were 37 popular votes cast for the first candidate and 56 votes cast for the second candidate. There can be multiple lines about the same state; for example, later in the file there might be a line of "NE 458 303" indicating that in another part of Nebraska, there were 458 votes cast for the first candidate and 303 votes cast for the second.

Your task is to count up who got the larger share of the popular vote in each state, printing this information to the console in the format shown below. Print information about the states in alphabetical order. Once you have printed who won each state and by what popular vote margin, print the total number of electoral votes won by each candidate along with who won the election in the format shown below.

Assumptions: To simplify the problem, you may assume that there are only two candidates. You may assume that there will not be any ties; the popular vote in a given state will not be a tie, and the overall electoral vote count will also not be a tie. You may assume valid input, meaning that both files will exist and will be in the format described here. You may assume that each state listed in the states input file will have at least one corresponding line of popular votes in the votes file, and that every line in the votes file will refer to a state that is listed in the states file.

The following table shows example contents of the two input files along with the results of an election conducted using this input data. This election has only four states' worth of data to keep things shorter for this example, but your function should work for any number of states.

states.txt votes.txt output from call of election("Tracy Flick", "Paul Metzler", "states.txt", "votes.txt");
CT 7
NE 5
AZ 9
KY 8
AZ 1041 949
KY 100 145
NE 37 56
CT 55 29
CT 118 156
NE 458 303
CT 26 31
CT 5 22
AZ 287 316
AZ: 9 electoral votes: Tracy Flick wins by 63
CT: 7 electoral votes: Paul Metzler wins by 34
KY: 8 electoral votes: Paul Metzler wins by 45
NE: 5 electoral votes: Tracy Flick wins by 136
Result: 14 to 15
Paul Metzler wins!
Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.