// This program has several errors that are to be found. // // Here is a requirements overview for this program // There has just been a hotly contested city council election. // In four voting precincts, citizens have cast their ballots for four // candidates. We wish to analyze the votes for these four candidates // by determining how many votes each candidate received in each precinct, // how manyh total votes each candidate received, and how many total votes // were cast in each precinct. // Input: An arbitrary number of votes in a file voteFile with each // vote represented as a pair of numbers: a precinct number ( 1 through // 4), and a candidate number ( 1 through 4); and candidate names, // entered through the keyboard to be used for output. // Output: A file reportFile containing: a table showing how many votes // each candidate recieved in each precinct, the total number of votes // for each candidate, and the total number of votes cast in each precinct. #include #include #include const int NUM_PRECINCTS = 4; const int NUM_CANDIDATES = 4; typedef char String10[11]; // 10 chars plus string terminator typedef int VoteTable[NUM_PRECINCTS] [NUM_CANDIDATES]; void GetNames( String10[]); void OpenForInput ( ifstream& ); void OpenForOutput ( ofstream& ); void WritePerCandidate ( const VoteTable, const String10[ ], ofstream&); void WritePerPrecinct ( const VoteTable, ofstream& ); void WriteTable ( const VoteTable, const String10[], ofstream&); void ZeroVotes ( VoteTable); int main ( ) { VoteTable votes; // Totals for precincts versus candidates String10 name[NUM_CANDIDATES]; // Candidate names int candidate; // candidate number from voteFile int precinct; // Precinct number from voteFile ifstream voteFile; // Input number of precincts, candidates ofstream reportFile; // Output file for summaries OpenForInput (voteFile); if ( !voteFile) return 1; OpenForOutput ( reportFile); if ( !reportFile) return 1; GetNames (name); ZeroVotes ( votes ); // Read and tally votes voteFile >> precinct >> candidate; while (voteFile) { votes[precinct ] [candidate ]++; voteFile >> precinct >> candidate; } // Write results to report file WriteTable ( votes, name, reportFile); WritePerCandidate ( votes, name, reportFile); WritePerPrecinct (votes, reportFile); return 0; } void OpenForInput ( ifstream& someFile) // Prompts for name of input file and opens it // Postcondition: // The user has been prompted for a file name and // if the file could not be opened, an error message // has been displayed. // Note: Upon return, the caller must test stream state { char fileName[51]; // User-specified file name of 50 chars or less cout << " Input File Name: "; cin.get(fileName, 51 ); cin.ignore ( 100, '\n'); someFile.open(fileName); if (!someFile) cout << " *** Can't open " << fileName << " ***" << endl; } void OpenForOutput ( ofstream someFile) // Prompts for name of output file and opens it // Postcondition: // The user has been prompted for a file name // and If the file could not be opened, an error message // has been displayed. // Note: Upon return, the caller must test stream state { char fileName[51]; // User specified file name of no more than 50 cout << "Output file name: "; cin.get (fileName, 51); cin.ignore ( 100, '\n'); someFile.open(fileName); if ( !someFile) cout << " *** Can't open " << fileName << " *** " << endl; } void GetNames ( String10 name[ ] ) { int candidate; // Loop counter cout << " ENter the names of the candidates, one per line. " << endl << " in the order they appear on the ballot." << endl; for ( candidate = 0; candidate < NUM_CANDIDATES; candidate++) { cin.get (name[candidate], 11); cin.ignore(100, '\n'); } } void ZeroVotes ( VoteTable votes ) { int precinct; // Loop counter int candidate; // Loop counter for ( precinct =1; precinct < NUM_PRECINCTS; precinct++) for ( candidate=0; candidate < NUM_CANDIDATES; candidate++) votes[precinct] [candidate] = 0; } void WriteTable ( const VoteTable votes, const String10 name[ ], ofstream& reportFile) { int precinct; // Loop counter int candidate; // Loop counter // Set up headings reportFile << " "; for ( candidate =0; candidate < NUM_CANDIDATES; candidate++) reportFile << setw(12) << name[candidate]; reportFile << endl; // Print table of total votes by row for (precinct = 0; precinct < NUM_PRECINCTS; precinct++) { reportFile << "Precinct" << setw(4) << precinct + 1; for ( candidate =0; candidate < NUM_CANDIDATES; candidate++) reportFile << setw(12) << votes[precinct] [candidate]; reportFile << endl; } void WritePerCandidate ( const VoteTable votes, const String10 name[ ], ofstream& reportFile ) { int precinct; // Loop counter int candidate; // Loop counter int total; // Total votes for a candidate for (candidate = 0; candidate < NUM_CANDIDATES; candidate++) { total = 0; // Computer column sum for ( precinct = 0; precinct < NUM_PRECINCTS; precinct++) total = total + votes[candidate][precinct]; reportFile << "Total votes for " << setw(10) << name[candidate] << ":" << setw(3) << total << endl; } reportFile << endl; } void WritePerPrecinct ( const VoteTable votes, ofstream& reportFile) { int precinct; // Loop counter int candidate; // Loop counter int total; // Total votes for a precinct for ( precinct = 0; precinct << NUM_PRECINCTS; precints++) { total = 0; // Compute row sum for ( candidate = 0; candidate <= NUM_CANDIDATES; candidate++) total = total + votes [ precinct] [ candidate]; reportFile << "Total votes for precinct " << setw(3) << precinct << ": " << setw(3) << total << endl; } }