Thread: Election program

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    46

    Election program

    Hey group, I have this new problem I am attempting to solve. I need to develop a program which computerrizes a voting process in an election.

    There are 4 regions where the votes are coming from. My output needs to give firstName, lastName, regionNumber, numberOfVotes.

    And then produce as output: Winner: Votes Received: & total votes polled:

    Also all of my names need to be in alphabetical order in the output

    There are six total canadates running for election.

    I have two input data files. One is the voteData.txt which consists of each candidates's name, region number and the numberts of votes received. one entry per line

    The other data file is candData.txt and it consists of names of candidates seeking president's post, in no particular order.



    We have not yet learned very in depth tactics. What would be the best approach to input the information, tabulate results, and then output in alphabetical order all info as well as results (ie winner)?

    Just help me get in the right direction, good tactics and organization techniques if you can guys. Thanks

    I will update you as I go or any problems I encounter, thanks again!!

    Justin

  2. #2
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    read up on linked lists

    Here

    Good luck.
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb

    I think the most optimal method for this type of data collection.. would be a simple array of structs. An array of 6 structs.. each containing data for first name, last name, a vote counter, and a numerical region (1-4):

    Code:
    struct Ballot
    {
    
         string first_name;
         string last_name;
         int vote;
         int region;
    
    }vote[5];

    Let each element represent an individual candidate.. Here is a rudimentary example of how you could manipulate this simple array of structs..
    Code:
         int entry;  
    
         cout << "Enter candidate (1-6) ";
         cin >> entry;
    
         --entry;    //compensate for the [0] zero element
    
         vote[entry].first_name = "John";
         vote[entry].last_name = "Kerry";
         vote[entry].++vote;
    
         cout << "Enter Region (1-4) ";
         cin >> vote[entry].region;

    And here is an example of how you could output this data:

    pseudocode
    Code:
    /* Use a sort algorithm to place the candidate with the highest
     number of votes..  in the highest element of the struct array.  You could
     write your own bubblesort algorithm easily.. but if you are 
    uncomfortable with sorting an array..    there is a sort( ) function 
    in the <algorithm> library that you can use..   which sorts an
     array in ascending order..  it uses the &first element..  and &last
     element as parameters.   Watch this example  */
    
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    int main()
    {
    
         sort(&vote[0].vote,  &vote[5].vote);     //Sort the struct array in ascending order based on number of votes
    
         
          //Now that the struct array is sorted, we know that the last element of the struct array
          //element[5] will always be the winning candidate  :)
    
         cout << "The winner is: " << vote[5].first_name << "  " << vote[5].last_name;

    As far is file I/O.. read up on this simple FAQ on how to use the <fstream> library.. and ifile >> to a .dat file.. and then read from that file


    Hope this simple tutorial on creating.. sorting.. and referencing an array of structs helps out.. it is a very good technique for storing a list of different information into simple, easy to referrence array I hope you consider this method.. it is not that difficult and it can really pay off.. especially if you don't want write a code intensive linked list
    Last edited by The Brain; 10-09-2004 at 02:26 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    thanks Brain, I appreciate it, I am reading up and putting this on paper and trying to figure some pieces out. I really appreciate your time, I will let you know how I progress!

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Ok, I am having a problem on how to read my input file into my program and filtering out the information I need. An example of the input file is listed below.

    Goldy Goofy 2 34
    Monty Mickey 1 56
    Ducky Donald 2 56
    Peter Pluto 1 78
    Docotor Doc 4 29
    Buddy Balto 4 78
    Monty Mickey 2 63


    --This input file contains the name of the person running for election, followed by the region the votes are coming from, followed by the number of votes.

    How can I read this input file and have my struct array filter out the information?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Also I have another text file to input from which is just simply the name of each candidate called canData.txt and it appears as:

    Goldy Goofy
    Monty Mickey
    Ducky Donald
    Peter Pluto
    Doctor Doc
    Budy Balto


    I am not sure why this input file is even being used. Only thing I can think of is that it is for organization and when I read the voteData.txt file which is all the voting information then we store it in one of these 6. Does this sound right?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    declare a struct type containing a name and a counter. Use default constructor to set counter to zero.

    declare an array or vector of struct

    declare an ifstream to read in names of candidates from candidate file assigning names, one for each struct

    close ifstream

    open ifstream and associate with the data file.

    In an outer loop use the >> operator to read in the data, one field at a time, into dummy variables. In an inner loop compare the name field of each candidate with dummy field of your choice. When you find a match increment the counter field of that candidate by the amount of the appropriate variable. Repeat outer loop as often as needed

    When done, sort the array based on the counter field and determine who won. Report the results.

    Post code that has you stumped, if it comes to that.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Ok, here is the whole code that I have came up with along with some help from others, and it will not work. Does anyone see anything in this code that is the hitch(s). The program will compile and link just wont do anything after it prompts the user to enter filenames. Please help in anyway you can, I am really stressing here! Thanks







    Code:
    #include <iostream>
    #include <fstream>
    #include<string>
    #include<cstddef>
    #include<iomanip>
    using namespace std;
    
    struct candInfo
    {
    	string lastName;
    	string firstName;
    	int Reg1;
    	int Reg2;
    	int Reg3;
    	int Reg4;
    	int total;
    };
    
    int main()
    {
    	ifstream inFile1, inFile2;
    	candInfo One, Two, Three, Four, Five, Six;
    	string fileName1, fileName2;
    
    	cout << "Enter name of file containing Candidate Names: ";
    	cin >> fileName1;
    	if (fileName1.fail()){
    		cout<<"ERROR! Input file not found, (location?)\n";
    		cout<<"Program Terminated\n";
    		exit(1);
    
    	inFile1.open(fileName1.c_str());
    	inFile1>>One.lastName;
    	inFile1>>One.firstName;
    	inFile1>>Two.lastName;
    	inFile1>>Two.firstName;
    	inFile1>>Three.lastName;
    	inFile1>>Three.firstName;
    	inFile1>>Four.lastName;
    	inFile1>>Four.firstName;
    	inFile1>>Five.lastName;
    	inFile1>>Five.firstName;
    	inFile1>>Six.lastName;
    	inFile1>>Six.firstName;
    
    	cout << "Enter name of file containing voting results:  ";
    	cin >> fileName2;
    	if (fileName2.fail()){
    		cout<<"ERROR! Input file not found, (location?)\n";
    		cout<<"Program Terminated\n";
    		exit(1);
    
    	inFile2.open(fileName2.c_str());
    	int numReg, numVotes;
    	string namCan;
    
    	One.Reg1=One.Reg2=One.Reg3=One.Reg4=One.total=0;
    	Two.Reg1=Two.Reg2=Two.Reg3=Two.Reg4=Two.total=0;
    	Three.Reg1=Three.Reg2=Three.Reg3=Three.Reg4=Three.total=0;
    	Four.Reg1=Four.Reg2=Four.Reg3=Four.Reg4=Four.total=0;
    	Five.Reg1=Five.Reg2=Five.Reg3=Five.Reg4=Five.total=0;
    	Six.Reg1=Six.Reg2=Six.Reg3=Six.Reg4=Six.total=0;
    
    	while(namCan != "end")
    	{
    		inFile2>>namCan;
    		inFile2>>numReg;
    		inFile2>>numVotes;
    		switch(numReg)
    		{
    		case 1:
    			One.Reg1+=numVotes;
    			break;
    		case 2:
    			One.Reg2+=numVotes;
    			break;
    		case 3:
    			One.Reg3+=numVotes;
    			break;
    		case 4:
    			One.Reg4+=numVotes;
    			break;
    		}
    	}
    	if(namCan == Two.lastName)
    	{
    		inFile2>>namCan;
    		inFile2>>numReg;
    		inFile2>>numVotes;
    		{
    			switch(numReg)
    		{
    		case 1:
    			Two.Reg1+=numVotes;
    			break;
    		case 2:
    			Two.Reg2+=numVotes;
    			break;
    		case 3:
    			Two.Reg3+=numVotes;
    			break;
    		case 4:
    			Two.Reg4+=numVotes;
    			break;
    		}
    	}
    		if(namCan == Three.lastName)
    		{
    			inFile2>>namCan;
    			inFile2>>numReg;
    			inFile2>>numVotes;
    			switch(numReg)
    		{
    		case 1:
    			Three.Reg1+=numVotes;
    			break;
    		case 2:
    			Three.Reg2+=numVotes;
    			break;
    		case 3:
    			Three.Reg3+=numVotes;
    			break;
    		case 4:
    			Three.Reg4+=numVotes;
    			break;
    		}
    	}
    		if(namCan == Four.lastName)
    		{
    			inFile2>>namCan;
    			inFile2>>numReg;
    			inFile2>>numVotes;
    			switch(numReg)
    		{
    		case 1:
    			Four.Reg1+=numVotes;
    			break;
    		case 2:
    			Four.Reg2+=numVotes;
    			break;
    		case 3:
    			Four.Reg3+=numVotes;
    			break;
    		case 4:
    			Four.Reg4+=numVotes;
    			break;
    		}
    	}
    		if(namCan == Five.lastName)
    		{
    			inFile2>>namCan;
    			inFile2>>numReg;
    			inFile2>>numVotes;
    			switch(numReg)
    		{
    		case 1:
    			Five.Reg1+=numVotes;
    			break;
    		case 2:
    			Five.Reg2+=numVotes;
    			break;
    		case 3:
    			Five.Reg3+=numVotes;
    			break;
    		case 4:
    			Five.Reg4+=numVotes;
    			break;
    		}
    	}
    		if(namCan == Six.lastName)
    		{
    			inFile2>>namCan;
    			inFile2>>numReg;
    			inFile2>>numVotes;
    			switch(numReg)
    		{
    		case 1:
    			Six.Reg1+=numVotes;
    			break;
    		case 2:
    			Six.Reg2+=numVotes;
    			break;
    		case 3:
    			Six.Reg3+=numVotes;
    			break;
    		case 4:
    			Six.Reg4+=numVotes;
    			break;
    		}
    	}
    	}
    
    		One.total=One.Reg1+One.Reg2+One.Reg3+One.Reg4;
    		Two.total=Two.Reg1+Two.Reg2+Two.Reg3+Two.Reg4;
    		Three.total=Three.Reg1+Three.Reg2+Three.Reg3+Three.Reg4;
    		Four.total=Four.Reg1+Four.Reg2+Four.Reg3+Four.Reg4;
    		Five.total=Five.Reg1+Five.Reg2+Five.Reg3+Five.Reg4;
    		Six.total=Six.Reg1+Six.Reg2+Six.Reg3+Six.Reg4;
    
    		cout<<setw(50)<<"---Election Results---"<<endl<<endl;
    		cout<<"Candidate Name" <<
    
    	setw(15)<<"Region1"<<setw(10)<<"Region2"<<setw(10)<<"Region3"<<setw(10)<<"Region4"<<
    	setw(10)<<"Total"<<endl;
    		cout<<"---------------" <<
    	setw(15)<<"--------"<<setw(10)<<"-------"<<setw(10)<<"-------"<<setw(10)<<"-------"<<
    	setw(10)<<"-------"<<endl;
    
    		cout<<Six.lastName<< 
    			setw(6)<<Six.firstName<<setw(17)<<Six.Reg1<<setw(10)<<Six.Reg2<<
    			setw(10)<<Six.Reg3<<setw(10)<<Six.Reg4<<setw(10)<<Six.total<<endl;
    		cout<<Five.lastName<<
    			setw(6)<<Five.firstName<<setw(17)<<Five.Reg1<<setw(10)<<Five.Reg2<<
    			setw(10)<<Five.Reg3<<setw(10)<<Five.Reg4<<setw(10)<<Five.total<<endl;
    		cout<<Three.lastName<<
    			setw(6)<<Three.firstName<<setw(17)<<Three.Reg1<<setw(10)<<Three.Reg2<<
    			setw(10)<<Three.Reg3<<setw(10)<<Three.Reg4<<setw(10)<<Three.total<<endl;
    		cout<<One.lastName<<
    			setw(6)<<One.firstName<<setw(17)<<One.Reg1<<setw(10)<<One.Reg2<<
    			setw(10)<<One.Reg3<<setw(10)<<One.Reg4<<setw(10)<<One.total<<endl;
    		cout<<Two.lastName<<
    			setw(6)<<Two.firstName<<setw(17)<<Two.Reg1<<setw(10)<<Two.Reg2<<
    			setw(10)<<Two.Reg3<<setw(10)<<Two.Reg4<<setw(10)<<Two.total<<endl;
    		cout<<Four.lastName<<
    			setw(6)<<Four.firstName<<setw(17)<<Four.Reg1<<setw(10)<<Four.Reg2<<
    			setw(10)<<Four.Reg3<<setw(10)<<Four.Reg4<<setw(10)<<Four.total<<endl;
    
    
    			candInfo Winner;
    			Winner=One;
    			if(Two.total>Winner.total)
    				Winner=Two;
    			if(Three.total>Winner.total)
    				Winner=Three;
    			if(Four.total>Winner.total)
    				Winner=Four;
    			if(Five.total>Winner.total)
    				Winner=Five;
    			if(Six.total>Six.total)
    				Winner=Six;
    
    			cout << "Winner:  "<<Winner.lastName<< " "<<Winner.firstName<<" "<<
    				"Votes Received: "<<Winner.total<<endl;
    			int totalVotes=One.total+Two.total+Three.total+Four.total+Five.total+Six.total;
    			cout<< "Total Votes Accounted For: "<<totalVotes<<endl;
    
    			return 0;
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Wow, you really need to learn what functions are and how to write them. That's really painful to read. Break it up into functions, and debug them one at a time. Much easier that way.

    You may want to initialize that string before you check it in your while loop also.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    quzah thanks for your reply, i know its ugly, how would you suggest breaking it down into functions and then calling those functions? I am a very new programmer and trying to get those things down. Could you break one down in a function to give me an example or something of the sort so I can see how and the logic behind it? Thanks

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Does anyone see anything that jumps out at them as to why this program wont execute correctly. The window will come up prompting user to enter filenames, however, after second filename is entered, the program stalls. Thanks in advance

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    if (fileName1.fail()){
     cout<<"ERROR! Input file not found, (location?)\n";
    		cout<<"Program Terminated\n";
    		exit(1);
    You need a closing curly bracket after the last line above in this snippet and in the corresponding one for fileName2 a little later.
    ____________________________________
    you need a line like this:

    inFile2.open(fileName2.c_str());

    between these two lines:

    cin >> fileName1;
    if (fileName1.fail())
    _________________________________
    you need to initialize namCan before you do this:

    while(namCan != "end")
    ____________________________________
    inFile2>>namCan;
    inFile2>>numReg;
    inFile2>>numVotes;

    I thought the results file had lines set up something like this:

    name1 name2 region votes

    If so you need to read all four pieces of information separately, putting the names in appropriate member variable and then do the name comparison to find the correct candidate.
    ___________________________________
    Code:
    while(namCan != "end")
    	{
      	inFile2>>namCan;
     	inFile2>>numReg;
     	inFile2>>numVotes;
    					switch(numReg)
      	{
    						case 1:
    				....
     	}
    	}
    the while loop ends here, which isn't where it should end. The results file is unlikely to end with the word "end", but who knows. Assuming it doesn't end with the word "end" then you need a different terminating condition for the while loop.
    _____________________________
    inFile2>>numVotes;
    switch(numReg)

    the curly bracket between these two lines is inappropriate.
    _____________________________
    There may be others, but I'll stop there for now. I agree with quzah---you could really benefit by better use of loops and possibly functions.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Thanks for your input elad, I am working on fixing the probelms you mentioned now. On your last input, you said there is a curly bracket between

    inFile2>>numVotes;
    switch(numReg)

    however, the bracket comes after the switch statement which is correct right?

    Also, the word "end" is not in the input file, I put that statement in there temporarily because of lack of a better way. How can I put a terminate condition for it to jump out of while loop. Would i use an EOF statement? I am not sure how to do that? Thanks

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    also the ".fail" statement is not being read by the compiler, do I have to include a special library at the beg. of code to allow the ".fail" statement to be used?

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Another thing, I keep getting this error, why?

    fatal error C1075: end of file found before the left brace '{'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM