
//*****************************************************************************
////  Course:             COMS 2203 Foundations of Computer Programming II Sect. 02
////  Semester:           Fall 2004
////  Assignment Number:  #3                             
////  Author Name:        DarC KonQuesT                        
////  Date Written:       September 16, 2004
////                                                         
////  Description of Program:                              
////  Take input from a file of votes for certain candidates in certain regions.
////  Tally and display vote results as well as winner and number of votes.                             
////*****************************************************************************
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

const int cand = 5;
const int regions = 4;

using namespace std;

int main()
{
  ifstream infile;
  string filename;
  int votes[cand][regions]; 
  int total_votes[cand]; 
  int k, j, vote_count, candidate, region, sum, col, row, temp, winner;
  

cout << "Enter input file name:  ";  
cin >> filename;
  infile.open(filename.c_str());
  if(!infile) {
    cerr << "Error: Can't open file." << endl;
    return 1;
  } 
  
 infile >> candidate >> region >> vote_count;

//read in values 
while(infile)
  {
    votes[candidate - 1][region - 1] = vote_count;
    infile >> candidate >> region >> vote_count;
  }


//total votes
row = 0;
while(row < cand)
  {
    sum = 0;
    for(col = 0; col < regions; col++)
      sum = sum + votes[row][col];
    total_votes[row] = sum;
    row++;
  }


//find winner
temp = total_votes[0];

for(k = 0; k < cand; k++)
{
  if(temp < total_votes[k])
  {
    temp = total_votes[k];
    winner = k + 1;
  }
}

//output graph
cout << endl << setw(30) << "Votes" << endl;
cout << setw(10) << "Candidate" << setw(10) << "Region1" << setw(10) << "Region2" << setw(10) << "Region3" << setw(10) << "Region4" << setw(10) << "Total" << endl;
cout << setw(10) << "---------" << setw(10) << "-------" << setw(10) << "-------" << setw(10) << "-------" << setw(10) << "-------" << setw(10) << "-----" << endl;

//fill graph with vote data
k = 0;
while(k < cand)
  {
    cout << setw(10) << k + 1;
    j = 0;
    while(j < regions)
    {
      cout << setw(10) << votes[k][j];
      j++;  
    }
    cout << setw(10) << total_votes[k] << endl;
    k++;
  }

//output the winner and amount of votes
cout << endl << "Winner:  " << winner << ", Votes Received: " << total_votes[winner -1] << endl << endl;

  return 0;
}


