Vote Counting Machine: Borda & Plurality

by bryan on November 5, 2009

This was my version of a C++ assignment to count votes from a text file. It’s not super useful outside the classroom, but I’m posting it anyway.

//
// Bryan Simonson
// Assignment #3
// Vote Counter
//

#include <fstream>
#include <string>
#include <iostream>
using namespace std;

// Function prototypes
int MenuSelect(void);
void LoadData(char vote[][100], int& candcount, int& votercount);
void PrefSched(char vote[][100], int& candcount, int& votercount);
void Plurality(char vote[][100], int& candcount, int& votercount);
void Borda(char vote[][100], int& candcount, int& votercount);
void PrintSpace(void);

int MenuSelect(){
	int menuoption = 0;

	cout << "\n\n\nMain Menu" << endl;
	cout << "==================================" << endl;
	cout << "1) Load Data" << endl;
	cout << "2) Preference Schedule" << endl;
	cout << "3) Plurality" << endl;
	cout << "4) Borda Count" << endl;
	cout << "5) Exit Program" << endl;
	cout << "==================================" << endl << endl;

	while (menuoption != 1 && menuoption != 2 && menuoption != 3 && menuoption != 4 && menuoption != 5){
		// Selection is invalid, prompt again
		cout << "Please enter a number between 1 and 5:" << endl;
		cin >> menuoption;
	}
	return menuoption;
}

void LoadData(char vote[][100], int& candcount, int& votercount){
	ifstream datafile;
	string filename, line;
	int cand = 0;
	int voter = 0;

	// Prompt for the filename. Unfortunately they'll have to type for now...
	cout << "Please type name of the file that contains vote data. (eg, \"votedata.txt\")" << endl;
	cin >> filename;

	// try to open file... if not, they're screwed (for now)
	datafile.open(filename.c_str());

	while (! datafile.eof()){
		getline(datafile,line);
		if (line.size() == 0){				// reached a blank line
			voter++;						// count a new voter
			cand = 0;						// reset "ballot" for new voter
		} else {
			vote[cand][voter] = line.at(0); // record vote
			cand++;
		}
	}

	// correct off by one issue from parsing empty lines
	voter = voter + 1;

	// set reference variables to the last count of voters/candidates
	candcount = cand;
	votercount = voter;

	// close the datafile
	datafile.close();

	cout << "Data loaded: "<< cand << " candidates, " << voter << " voters\n\n" << endl;
}

void PrefSched(char vote[][100], int& candcount, int& votercount){
	// Preference Schedule
	// Need to find all the different voting combos
	// and tell the user how many times they appeared

	// preference is a voter attribute
	// to get something comparable we need to create a string by concatenating each column
	string voterpref,comparestring;
	int score;

	for (int i=0;i<votercount;i++){
		voterpref = "";		//reset compare string
		for (int n=0;n<candcount;n++){
			voterpref.push_back(vote[n][i]);

			// time to compare
			score = 0;
			if (n == (candcount-1)){
				// end of column, we have the full string so let's compare

				for (int x=0;x<votercount;x++){
					// reset compare string
					comparestring = "";

					for (int y=0;y<candcount;y++){
						// generate same string for comparison purposes
						comparestring.push_back(vote[y][x]);

						if (y == (candcount-1)){
							if (voterpref == comparestring){
								score++;
							}
						}
					}
				}

				// finally, let's print the result
				cout << voterpref << " - " << score << endl;
			}
		}
	}
}

void Plurality(char vote[][100], int& candcount, int& votercount){
	int score;

	cout << "==========================" << endl;
	cout << "Plurality Count" << endl;
	cout << candcount << " candidates, " << votercount << " voters" << endl;
	cout << "==========================" << endl;

	// Need to count first place votes for each candidate
	for (int n = 0; n<candcount; n++){
		score = 0;
		for (int i = 0; i<votercount; i++){
			// compare first column to first row
			if (vote[n][0] == vote[0][i]){
				score++;
			}
		}
		cout << vote[n][0] << " - " << score << endl;
	}
}

void Borda(char vote[][100], int& candcount, int& votercount){
	// Borda scoring
	// Scoring depends on number of candidates
	int score = 0;

	cout << "==========================" << endl;
	cout << "Borda Count" << endl;
	cout << candcount << " candidates, " << votercount << " voters" << endl;
	cout << "==========================" << endl;

	// we want to print out each candidate's score only once, so loop through candidates
	for (int i = 0; i<candcount; i++){
		score = 0;
		cout << vote[i][0] << " - ";

		// for each candidate we have to go through the entire table for matches
		// this time as x gets higher, preference (multiple) gets lower
		for (int x = 0, multiple = candcount; x<candcount; x++, multiple--){
			for (int n = 0; n<votercount; n++){
				if (vote[i][0] == vote[x][n]){
					// match found, add points to score
					score += multiple;
				}
			}
		}
		cout << score << endl;
	}
}

void PrintSpace(void){
	cout << "\n\n\n\n\n\n\n\n\n\n";
}

int main(){
	int menuoption;// = MenuSelect();
	bool exitprogram = false;		// default (of course) to not exiting
	int candcount = 0;
	int votercount = 0;
	char vote[7][100];

	do {
		menuoption = MenuSelect();	// set the menuselection
		switch (menuoption){
			case 1: PrintSpace(); LoadData(vote, candcount,votercount); break;
			case 2: PrintSpace(); PrefSched(vote, candcount, votercount); break;
			case 3: PrintSpace(); Plurality(vote, candcount, votercount); break;
			case 4: PrintSpace(); Borda(vote, candcount, votercount); break;
			case 5: exitprogram = true; break;
			default: cout << "Make another selection.\n\n"; break;
		}
	} while (! exitprogram);

	// Give user an airline goodbye
	cout << "Buh-bye now." << endl;

	return 0;
}

Leave a Comment

Connect with Facebook

Previous post:

Next post: