Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,550 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,579 people online right now. Registration is fast and FREE... Join Now!




Election program question

 
Reply to this topicStart new topic

Election program question

camperxl
30 Oct, 2007 - 07:22 PM
Post #1

New D.I.C Head
*

Joined: 30 Oct, 2007
Posts: 2


My Contributions
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


Attached File(s)
Attached File  tie.txt ( 68bytes ) Number of downloads: 30
User is offlineProfile CardPM
+Quote Post

MikeRaines
RE: Election Program Question
30 Oct, 2007 - 08:26 PM
Post #2

D.I.C Head
**

Joined: 30 Oct, 2007
Posts: 76



Thanked: 1 times
My Contributions
Without looking at your code in great detail one thing caught my eye witha quick glance.

CODE

for(int i = 0; i < counter; i++)
    {
        ptr[i].max(maximum,winner);                                                        //calls the max function
    }
    //Something Here?????
    {
        cout <<"The winner is: " <<winner <<endl <<endl;
        return 0;
    }


You have that section of code bracketed like you wanted to logiclly enter it, but I don't see any. Perhaps I missed it, or perhaps thats your problem with the double print. If that is not it I would be happy to look more closely.
User is offlineProfile CardPM
+Quote Post

camperxl
RE: Election Program Question
31 Oct, 2007 - 04:55 AM
Post #3

New D.I.C Head
*

Joined: 30 Oct, 2007
Posts: 2


My Contributions
Sorry - that's a left over code snippet where I was trying different things to try to resolve the issue. Since the brackets didnt change anything I took a shortcut and just left them in. A deeper look would be appreciated.

QUOTE(MikeRaines @ 30 Oct, 2007 - 09:26 PM) *

Without looking at your code in great detail one thing caught my eye witha quick glance.

CODE

for(int i = 0; i < counter; i++)
    {
        ptr[i].max(maximum,winner);                                                        //calls the max function
    }
    //Something Here?????
    {
        cout <<"The winner is: " <<winner <<endl <<endl;
        return 0;
    }


You have that section of code bracketed like you wanted to logiclly enter it, but I don't see any. Perhaps I missed it, or perhaps thats your problem with the double print. If that is not it I would be happy to look more closely.


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:45PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month