///////////////////////////////////////////////// // Vote Counter // // Implemented by: Adam Barth // // Designed by: Rachel Parke-Houben // // October 17, 2004 // ///////////////////////////////////////////////// // Include standard library #include // Use std namespace to get cin, cout, and endl using namespace std; ///////////////////////////////////////////////// // Macro Definitions // ///////////////////////////////////////////////// // This is declared in some header, but I forget where #ifndef EXIT_SUCCESS #define EXIT_SUCCESS 0 #endif // Each candidate has a letter of the alphabet #define KERRY 'K' #define BUSH 'B' #define NADER 'N' ///////////////////////////////////////////////// // voteCounter Declaration // ///////////////////////////////////////////////// ///////////////////////////////////////////////// // voteCounter // // Counts votes for each candidate class voteCounter { public: // Constructor voteCounter( void ); // Destructor (declare virtual for derived classes) virtual ~voteCounter(); // Count a vote for one of the three candidates void incKerry( void ); void incBush( void ); void incNader( void ); // Or else count a vote for Other void incOther( void ); // Print the results of the election void printResults( void ) const; private: unsigned long // Candidates // Statistics m_kerry, m_total, m_bush, m_other, m_nader; }; ///////////////////////////////////////////////// // voteCounter Implementation // ///////////////////////////////////////////////// ///////////////////////////////////////////////// // Constructor // // Initalize all vote counts to zero voteCounter::voteCounter( void ) : m_total(0), m_kerry(m_total), m_bush( m_total), m_nader(m_total), m_other(m_total) { } ///////////////////////////////////////////////// // Destructor // // Nothing to destruct voteCounter::~voteCounter() { } ///////////////////////////////////////////////// // incKerry // // Count a vote for Kerry void voteCounter::incKerry( void ) { m_kerry++; // Record vote m_total++; // Increment total } ///////////////////////////////////////////////// // incBush // // Count a vote for Bush void voteCounter::incBush( void ) { m_bush++; // Record vote m_total++; // Increment total } ///////////////////////////////////////////////// // incNader // // Count a vote for Nader void voteCounter::incNader( void ) { m_nader++; // Record vote m_total++; // Increment total } ///////////////////////////////////////////////// // incOther // // Count a vote for Other void voteCounter::incOther( void ) { m_other++; // Record vote m_total++; // Increment total } ///////////////////////////////////////////////// // printResults // // Print the results of the election void voteCounter::printResults( void ) const { cout << "Kerry: " << m_kerry << endl; // Print Kerry's total cout << "Bush: " << m_bush << endl; // Print Bush's total cout << "Nader: " << m_nader << endl; // Print Nader's total cout << "Other: " << m_other << endl; // Print Other's total } ///////////////////////////////////////////////// // main // // Read input and drive voteCounter int main( int argc, char** argv ) { voteCounter votes; // Until we reach the end-of-file while( !cin.eof() ) { unsigned char v; // Read in a vote cin >> v; // Spaces don't count as votes if( isspace(v) ) continue; // Who is this vote for? switch(v) { case KERRY: votes.incKerry(); break; // Record vote for Kerry case BUSH: votes.incBush(); break; // Record vote for Bush case NADER: votes.incNader(); break; // Record vote for Nader default: votes.incOther(); break; // Record vote for Other } } // Print the results of the election votes.printResults(); // Exit successfully return EXIT_SUCCESS; }