//function prototypes
void readVotes(ifstream &, int [], int [], int );
int calcTotalVotes(int[], int );
void printVotingResults(int[], int[], int, int , int);
int main ()
{
//variable declaration
int NUM_VOTING_SITES; //number of voting sites
ifstream fin;
string fileName; //name of file to be opened
//read in file name
cout << "Enter name of file: ";
cin >> fileName;
//open voting file
fin.open(fileName.c_str());
if (!fin.is_open())
{ //file not openned
cout << "Unable to open file named <" << fileName << ">\n";
}
else
{ //file opened
//read number of voting sites from the file
fin >> NUM_VOTING_SITES;
//declare arrays for votes fore each site
int demVotes[NUM_VOTING_SITES]; //votes for Democratic candidate by polling station
int repVotes[NUM_VOTING_SITES]; //votes for Rebublican candidate by polling station
//read votes for each voting site for each candidte
readVotes(fin, demVotes, repVotes, NUM_VOTING_SITES);
//close file
fin.close();
//compute totals for each candidate
int totalDemVotes = calcTotalVotes(demVotes, NUM_VOTING_SITES);
int totalRepVotes = calcTotalVotes(repVotes, NUM_VOTING_SITES);
//print totals and winner
printVotingResults(demVotes, repVotes, totalDemVotes, totalRepVotes, NUM_VOTING_SITES);
//determine winner
if (totalDemVotes > totalRepVotes)
cout << "Democrat is winner" << endl;
else if (totalRepVotes > totalDemVotes)
cout << "Republican is winner" << endl;
else
{
cout << "Election is a tie" << endl;
srand(22);
int num = rand( );
cout << "Random number using seed 22 is " << num << endl << endl;
if (num%2 == 0)
cout << "Democratic candidate wins on a flip (even)" << endl;
else
cout << "Republican candidate wins on a flip (odd)" << endl;
}
}
//freeze console window and halt
cout << endl;
system("Pause");
return 0;
} //end main
//read votes for all sites for both candidate
void readVotes(ifstream& fin, int demVotes[ ], int repVotes[], int numSites)
{
for (int n=0; n<numSites; n++)
{
fin >> demVotes[n];
fin >> repVotes[n];
}
}
//compute total votes from all sites for one party
int calcTotalVotes(int votes[], int numSites)
{
int total = 0;
for (int n=0; n<numSites; n++)
{
total += votes[n];
}
return total;
}
//print results per voting site and winnter
void printVotingResults(int demVotes[ ], int repVotes[], int totalDemVotes, int totalRepVotes, int numSites)
{
cout << endl;
cout << " 1 2 3" << endl;
cout << "123456789012345678901234567890" << endl;
cout << "Site Democratic Republican" << endl;
//print votes for each site
for (int k=0; k<numSites; k++)
{
cout << setw(3) << (k+1);
cout << setw(9) << demVotes[k];
cout << setw(12) << repVotes[k] << endl;
}
//Course: 4002-208
//Author: Rayno Niemi
//Purpose: Implementation of general input routines
#include <iostream>
#include <string>
using namespace std;
//---------------------------------
//Name: readDouble
//Purpose: read in a double based on the prompt
//Parameters:
// prompt - message to display to user
//Returns: input value
//---------------------------------
double readDouble(string prompt)
{
double number;
//read first input
cout << prompt;
cin >> number;
//check if input is a valid number
while (cin.fail())
{ //improperly formatted double
cout << "Invalid format for double number\n";
cin.clear(); //reset status bits
cin.ignore(100, '\n'); //flush input buffer with improperly formatted number
//read another input
cout << prompt;
cin >> number;
}
number = readDouble(prompt);
while(number < min || number > max)
{
cout << "\tInvalid Input - must be between " << min << " and "<< max << endl;
number = readDouble(prompt);
}
//---------------------------------
int readIntPos(string prompt)
{
int number;
//call basic double read to get valid double
number = readInt(prompt);
//have a valid intger if it is positive
while(number <= 0.0)
{
cout << "\tInvalid Input - must be positive" << endl;
number = readInt(prompt);
}
return number;
}
//---------------------------------\\
int readIntBetween(string prompt, int min, int max)
{
int number;
number = readInt(prompt);
while(number < min || number > max)
{
cout << "\tInvalid Input - must be between " << min << " and "<< max << endl;
number = readInt(prompt);
}