Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ General Description: Write a program that reads data on election returns from a file called precincts.txt. The file contains candidate names and a list

C++
image text in transcribed
image text in transcribed
image text in transcribed
General Description: Write a program that reads data on election returns from a file called precincts.txt. The file contains candidate names and a list of votes by individual precincts (details below). The program then calculates the sum of votes for each candidate. Next, the program should sort the precinct data by precinct number. Finally, the program writes a report to a file called results.txt as described below. Coding Specifications: data structures. The names of the 3 candidates, number of precincts (max 20), and total votes for each of the 3 candidates may be stored in simple variables. Write a structure for a precinct that has 4 integer members: - a precinct number number of votes for the first candidate - number of votes for the second candidate number of votes for the third candidate The program should declare a Partial Array of precinct structures (max 20). It should read precinct data from the input file into the Partial Array, perform calculations on it, sort it by precinct number, and finally write it to the report. Input File: precincts.txt The first three lines contain the names of the three candidates, one name per line. Since the names contain spaces, f>>namel will not work; instead use the getline() function to read each name: getline (f, namel); // f is the already-opened file handle The fourth line contains the number of precincts (max 20). The remaining lines contain four numbers each, with data for one precinct: o precinct number o number of votes for candidate 1 o number of votes for candidate 2 o number of votes for candidate 3 - Sample input data files: the program should work for either of these Arnold Anderson Richard Flair Roddy Piper Arnold Anderson Richard Flair Roddy Piper 303 123 241 29 101 98 306 15 202 73 41 125 505 347 290 29 404 41 76 4 707 231 412 92 606 481 112 115 808 43 51 6 In the first example, in Precinct #303, there were 123 votes for Anderson, 241 for Flair and 29 for Piper. Output File: It should be a report in the following format: - the long dashed lines (1 and 3) are 56 dashes each ELECTION RESULTS is centered between these two on line 2 - Column Header Dashed Lines: o 8 dashes under PRECINCT o 15 dashes under each candidate name o 1 space between each column Precinct numbers and candidate names are LEFT justified; other numbers RIGHT justified Candidate names are in field widths of 15 with one space between Note the data is sorted by precinct number Sample: based on first sample input file above See code, printing to screen instead of file, provided on course website (formatting help) ELECTION RESULTS PRECINCT Arnold Anderson Richard Flair Roddy Piper 306 41 101 202 383 ARA 125 123 29 ses 241 76 290 954 347 682 TOTALS 2e2 Execution: When opening the files, the program should check for failure. When a failure occurs, print "Failure to open filename." and end the program. A loop will be needed to read the precinct data from the file. Do not total the votes or try to sort the data while it is being read. Those tasks should be coded later in the program. Note the temp variable for the swap of the sort will be of data type precinct, not int! When successful, the program simply prints "Election data processed." and does a system pause. Execution Output: CAUsersKevin\Documents\HTML\cs215Wabs labs Election data processed. Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions