Scenario:Election program being fed a input text file with candidate's name followed by number of votes. Example:
Bill 16000
Tom 1500
Mark 16000
John 1200
Cranston 2300
Problem:In a tie situation (as above) my program displays "There is a tie!" then immediately below that "The winner is: Bill". This is obviously not the case and a problem. It operates normally in a non-tie situation.
Requested Solution:Can someone please assist (code preferred, as opposed to advice - but I'll take whatever is offered) in how I can accomplish the following - 1) correcting the incorrect double printing of "There is a tie!" followed by "The winner is: Bill" 2)Implementing, in a tie situation, the printing of "There is a tie between: Name1, Name2, Name3 etc". I've put a lot of work in on this program and am almost there, but need a little help as I'm not sure how to accomplish my goal.
My code:CODE
/ ********************************************************************************
*******
main
********************************************************************************
********/
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "election.h"
using namespace std;
//functions outside of main
int getinfo();
void dynamic(candidate* &ptr,int& size);
//Main function calls the getinfo function
int main()
{
getinfo();
}
//getinfo function - reads the input file and calls the print function to print the candidate(s) name and votes, then calls the dynamic function
int getinfo()
{
int votes; //votes for each candidate
int totalvotes = 0; //total votes for each candidate
int counter = 0; //temporary variable that keeps count
int maximum = 0; //temporary variable
int size = 5; //maximum size for the array
string file; //name of the data file being read
string winner; //winner of the election
string candidatename; //candidate name
candidate *ptr; //pointer
ifstream in; //variable for the infile
ptr = new candidate[size]; //creates an array for the pointer ptr
cout <<"Please enter the data filename: "; //prompts for the file name
cin >> file; //accepts user input
cout <<endl <<endl;
in.open(file.c_str(),ios::in); //opens the infile
while(!in) //checks if the infile name is valid
{
cout <<"\nThe data file could not be opened!\nPlease re-enter the data file name: ";
in.clear();
cin >> file;
in.open(file.c_str(),ios::in);
}
cout <<endl <<endl <<" Candidate " <<setw(18) <<" Votes Received " <<setw(15) <<" Percentage of Votes Received" <<endl;
cout <<"-------------------------------------------------------------------------\n";
while(in >> candidatename >> votes) //while infile has candidates and the votes received
{
totalvotes = totalvotes + votes; //equation for totaling totalvotes
ptr[counter].getdata(candidatename,votes); //gets the data for that candidate
counter = counter + 1; //increments the counter
if(counter == size)
dynamic(ptr,size); //calls the dynamic function
}
for(int i = 0; i < counter; i++)
{
ptr[i].print(totalvotes); //calls the print function
}
cout <<endl <<" --------------------\n";
cout <<"Total Votes: \t " <<totalvotes <<endl;
for(int i = 0; i < counter; i++)
{
ptr[i].max(maximum,winner); //calls the max function
}
{
cout <<"The winner is: " <<winner <<endl <<endl;
return 0;
}
}
//Dynamic Function - receives the pointer ptr and integer size
void dynamic(candidate* &ptr,int& size)
{
candidate* ptr2; //pointer ptr2
ptr2 = new candidate[size+5]; //creates an array for the pointer ptr2
for(int i = 0; i < size; i++)
ptr2[i] = ptr[i]; //sets pointer ptr2 equal to ptr
size = size + 5; //size equals size plus 5
delete[] ptr; //deletes the pointer ptr
ptr = ptr2; //sets pointer ptr equal to ptr2
}
/*************************************************************************
header
*************************************************************************/
#include <string>
#ifndef H_candidate
#define H_candidate
using namespace std;
class candidate
{
private:
string name; //name of the candidate
int numvotes; //number of votes for the candidate
public: candidate(); //constructor
void getdata(string, int); //function that gets the data
int max(int&, string&); //function for the max votes or tie
void print(int); //prints candidate name, total votes, and percentage to the screen
};
#endif
/*************************************************************************
Implementation
*************************************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "election.h"
using namespace std;
candidate::candidate() //constructor that initializes name to blank and numvotes to 0
{
name = " ";
numvotes = 0;
}
void candidate::getdata(string candidatename, int votes) //sets the name of the candidate and the number of votes
{
name = candidatename;
numvotes = votes;
}
void candidate::print(int totalvotes) //prints the candidate's name, num of votes, and % to the screen
{
float percentage; //% of votes variable
percentage = static_cast<float>(numvotes * 1.0 / totalvotes) * 100; //calculates the percentage of votes
cout <<fixed <<showpoint;
cout <<setprecision(2);
cout <<setw(10) <<name <<setw(15) <<numvotes;
cout <<setw(15);
cout <<percentage <<endl;
}
int candidate::max(int& max,string& winner) //calculates the maximum number of votes and if there is a tie
{
if(numvotes > max)
{
max = numvotes;
winner = name;
return 0;
}
else if(numvotes == max)
{
cout <<"There is a tie!" <<endl;
return 1;
}
}
This post has been edited by camperxl: 30 Oct, 2007 - 07:25 PM