I have a problem printing the winners in my Voting Program. Please help me finish it.

#include <iostream>
using namespace std;

void DisplayOptions();
//void DisplayOptionsVice();
int InputVote();
int UpdatePoll();
void DisplayPoll();
//void DisplayPollVice();

int VoteArray[5];

//-----------------------------

void main()
{
	int option;
	int loop;
	int ctr=0;
	
	for(loop=0; loop<6; ++loop)
	{
		VoteArray[loop]=0;
	}
	
	do
	{
		DisplayOptions();
		option=UpdatePoll();
		DisplayPoll();
		ctr++;
	} while (ctr<10);

	/*for(loop=0; loop<6; ++loop)
	{
		VoteArray[loop]=0;
	}
	
	do
	{
		DisplayOptionsVice();
		option=UpdatePoll();
		DisplayPollVice();
		ctr1++;
	} while (ctr<10);*/
}

//-----------------------------

void DisplayOptions()
	{
	cout<<"The Student Council Online Voting System"<<endl<<endl
		<<"*List of students running as President*"<<endl<<endl
		<<"Options are:"<<endl<<endl
		<<"Press 1 for Chariza Lyn Lopez."<<endl
		<<"Press 2 for Mark Limtian."<<endl
		<<"Press 3 for Johnvic Pronaci."<<endl
		<<"Press 4 for Michael Benjamin Rubio."<<endl
		<<"Press 5 for Joseph Tan."<<endl<<endl;
	}

//------------------------------

int InputVote()
{
	int option;
	cin>>option;
	return option;
};

//------------------------------

int UpdatePoll()
{
	int option;

	option=InputVote();

	if ((option>=0) && (option<6))
		VoteArray[0]++;
	else
		cout<<endl<<"Illegal entry, please re-enter."<<endl<<endl;

	switch (option)
	{
		case 1:
			VoteArray[1]++;
			break;

		case 2:
			VoteArray[2]++;
			break;

		case 3:
			VoteArray[3]++;
			break;

		case 4:
			VoteArray[4]++;
			break;

		case 5:
			VoteArray[5]++;
			break;

	}
	return option;
}

//-----------------------------

void DisplayPoll()
{
	int loop=0;

	cout<<endl<<endl
	<<"Total votes cast: "<<VoteArray[0]<<endl<<endl;

	cout<<"Chariza Lyn Barrameda:   |";
	for (loop=0; loop<VoteArray[1]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[1]<<endl;

	cout<<"Mark Limtian:            |";
	for (loop=0; loop<VoteArray[2]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[2]<<endl;

	cout<<"Johnvic Pronaci          |";
	for (loop=0; loop<VoteArray[3]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[3]<<endl;

	cout<<"Michael Benjamin Rubio:   |";
	for (loop=0; loop<VoteArray[4]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[4]<<endl;

	cout<<"Joseph Tan:               |";
	for (loop=0; loop<VoteArray[5]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[5]<<endl;

	cout<<endl<<"--------------------------"<<endl;

	if(VoteArray[1]>VoteArray[2]&&VoteArray[1]>VoteArray[3]&&VoteArray[1]>VoteArray[4]&&VoteArray[1]>VoteArray[5])	
		cout<<"The elected President is Ms. Charizza Lyn Lopez"<<endl<<endl<<endl<<endl;
	if(VoteArray[2]>VoteArray[1]&&VoteArray[2]>VoteArray[3]&&VoteArray[2]>VoteArray[4]&&VoteArray[2]>VoteArray[5])
		cout<<"The elected President is Mr. Mark Limtian"<<endl<<endl<<endl<<endl;
	if(VoteArray[3]>VoteArray[1]&&VoteArray[3]>VoteArray[2]&&VoteArray[3]>VoteArray[4]&&VoteArray[3]>VoteArray[5])
		cout<<"The elected President is Mr. Johnvic Pronaci"<<endl<<endl<<endl<<endl;
	if(VoteArray[4]>VoteArray[1]&&VoteArray[4]>VoteArray[2]&&VoteArray[4]>VoteArray[3]&&VoteArray[4]>VoteArray[5])
		cout<<"The elected President is Mr. Michael Benjamin Rubio"<<endl<<endl<<endl<<endl;
	if(VoteArray[5]>VoteArray[1]&&VoteArray[5]>VoteArray[2]&&VoteArray[5]>VoteArray[3]&&VoteArray[5]>VoteArray[4])
		cout<<"The elected President is Mr. Joseph Tan"<<endl<<endl<<endl

	}
/*void DisplayOptionsVice()
	{
	cout<<"The Student Council Online Voting System"<<endl<<endl
		<<"*List of students running as Vice President*"<<endl<<endl
		<<"Options are:"<<endl<<endl
		<<"Press 1 for Elmer Angeles."<<endl
		<<"Press 2 for E.R. Noel Manaois."<<endl
		<<"Press 3 for Joannabel Pronaci."<<endl
		<<"Press 4 for Roselyn Joy Rono."<<endl
		<<"Press 5 for Kevin Edward Vicencio."<<endl<<endl;
	}
int UpdatePoll();
void DisplayPollVice()
{
	int loop;
	
	cout<<endl<<endl
	<<"Total votes cast: "<<VoteArray[0]<<endl<<endl;

	cout<<"Elmer Angeles:   |";
	for (loop=0; loop<VoteArray[1]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[1]<<endl;

	cout<<"E.R. Noel Manaois:            |";
	for (loop=0; loop<VoteArray[2]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[2]<<endl;

	cout<<"Joannabel Pronaci          |";
	for (loop=0; loop<VoteArray[3]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[3]<<endl;

	cout<<"Roselyn Joy Rono:   |";
	for (loop=0; loop<VoteArray[4]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[4]<<endl;

	cout<<"Kevin Edward Vicencio:               |";
	for (loop=0; loop<VoteArray[5]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[5]<<endl;

	cout<<endl<<"--------------------------"<<endl;
	
	if(VoteArray[1]>VoteArray[2]&&VoteArray[1]>VoteArray[3]&&VoteArray[1]>VoteArray[4]&&VoteArray[1]>VoteArray[5])	
		cout<<"The elected Vice President is Mr. Elmer Rhoel Angeles."<<endl<<endl<<endl<<endl;
	if(VoteArray[2]>VoteArray[1]&&VoteArray[2]>VoteArray[3]&&VoteArray[2]>VoteArray[4]&&VoteArray[2]>VoteArray[5])
		cout<<"The elected Vice President is Mr. E.R. Noel Manaois."<<endl<<endl<<endl<<endl;
	if(VoteArray[3]>VoteArray[1]&&VoteArray[3]>VoteArray[2]&&VoteArray[3]>VoteArray[4]&&VoteArray[3]>VoteArray[5])
		cout<<"The elected Vice President is Mr. Joannabel Pronaci."<<endl<<endl<<endl<<endl;
	if(VoteArray[4]>VoteArray[1]&&VoteArray[4]>VoteArray[2]&&VoteArray[4]>VoteArray[3]&&VoteArray[4]>VoteArray[5])
		cout<<"The elected Vice President is Ms. Roselyn Joy Rono."<<endl<<endl<<endl<<endl;
	if(VoteArray[5]>VoteArray[1]&&VoteArray[5]>VoteArray[2]&&VoteArray[5]>VoteArray[3]&&VoteArray[5]>VoteArray[4])
		cout<<"The elected Vice President is Mr. Kevin Edward Vicencio."<<endl<<endl<<endl<<endl;
}*/

Recommended Answers

All 6 Replies

What is specifically wrong with the code?

Also, look at line 26, your array has 5 elements VoteArray[0],VoteArray[1],VoteArray[2],VoteArray[3],VoteArray[4] . Your loop is trying to access element 5 which doesn't exist.

The program is running, it's just that every time I select a candidate it automatically prints the winner. I should only print the winner after 10 votes.

Here's the cleaner code:

#include <iostream>
using namespace std;

void DisplayOptions();
int InputVote();
int UpdatePoll();
void DisplayPoll();


int VoteArray[5];

//-----------------------------

void main()
{
	int option;
	int loop;
	int ctr=0;
	
	for(loop=0; loop<6; ++loop)
	{
		VoteArray[loop]=0;
	}
	
	do
	{
		DisplayOptions();
		option=UpdatePoll();
		DisplayPoll();
		ctr++;
	} while (ctr<10);

}

//-----------------------------

void DisplayOptions()
	{
	cout<<"The Student Council Online Voting System"<<endl<<endl
		<<"*List of students running as President*"<<endl<<endl
		<<"Options are:"<<endl<<endl
		<<"Press 1 for Chariza Lyn Lopez."<<endl
		<<"Press 2 for Mark Limtian."<<endl
		<<"Press 3 for Johnvic Pronaci."<<endl
		<<"Press 4 for Michael Benjamin Rubio."<<endl
		<<"Press 5 for Joseph Tan."<<endl<<endl;
	}

//------------------------------

int InputVote()
{
	int option;
	cin>>option;
	return option;
};

//------------------------------

int UpdatePoll()
{
	int option;

	option=InputVote();

	if ((option>=0) && (option<6))
		VoteArray[0]++;
	else
		cout<<endl<<"Illegal entry, please re-enter."<<endl<<endl;

	switch (option)
	{
		case 1:
			VoteArray[1]++;
			break;

		case 2:
			VoteArray[2]++;
			break;

		case 3:
			VoteArray[3]++;
			break;

		case 4:
			VoteArray[4]++;
			break;

		case 5:
			VoteArray[5]++;
			break;

	}
	return option;
}

//-----------------------------

void DisplayPoll()
{
	int loop=0;

	cout<<endl<<endl
	<<"Total votes cast: "<<VoteArray[0]<<endl<<endl;

	cout<<"Chariza Lyn Barrameda:   |";
	for (loop=0; loop<VoteArray[1]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[1]<<endl;

	cout<<"Mark Limtian:            |";
	for (loop=0; loop<VoteArray[2]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[2]<<endl;

	cout<<"Johnvic Pronaci          |";
	for (loop=0; loop<VoteArray[3]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[3]<<endl;

	cout<<"Michael Benjamin Rubio:   |";
	for (loop=0; loop<VoteArray[4]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[4]<<endl;

	cout<<"Joseph Tan:               |";
	for (loop=0; loop<VoteArray[5]; ++loop)
		cout<<"-";
	cout<<"*  "<<VoteArray[5]<<endl;

	cout<<endl<<"--------------------------"<<endl;
	


	if(VoteArray[1]>VoteArray[2]&&VoteArray[1]>VoteArray[3]&&VoteArray[1]>VoteArray[4]&&VoteArray[1]>VoteArray[5])	
		cout<<"The elected President is Ms. Charizza Lyn Lopez"<<endl<<endl<<endl<<endl;
	if(VoteArray[2]>VoteArray[1]&&VoteArray[2]>VoteArray[3]&&VoteArray[2]>VoteArray[4]&&VoteArray[2]>VoteArray[5])
		cout<<"The elected President is Mr. Mark Limtian"<<endl<<endl<<endl<<endl;
	if(VoteArray[3]>VoteArray[1]&&VoteArray[3]>VoteArray[2]&&VoteArray[3]>VoteArray[4]&&VoteArray[3]>VoteArray[5])
		cout<<"The elected President is Mr. Johnvic Pronaci"<<endl<<endl<<endl<<endl;
	if(VoteArray[4]>VoteArray[1]&&VoteArray[4]>VoteArray[2]&&VoteArray[4]>VoteArray[3]&&VoteArray[4]>VoteArray[5])
		cout<<"The elected President is Mr. Michael Benjamin Rubio"<<endl<<endl<<endl<<endl;
	if(VoteArray[5]>VoteArray[1]&&VoteArray[5]>VoteArray[2]&&VoteArray[5]>VoteArray[3]&&VoteArray[5]>VoteArray[4])
		cout<<"The elected President is Mr. Joseph Tan"<<endl<<endl<<endl;
	
}

Please help me. I really need to make this program run.

I meant line 20, not line 26, sorry. But you are also trying to increment an element that's out of bounds on line 90. Your array elements go from 0 to 4 for a 5 element array. Fix that and I'll look at the rest of it.

Move displayPoll() outside of your while loop if you only want it to display once.

Also, main() is never void, it always returns an int.

I see what you are trying to do with the first element of the array. Well, make it of length 6 instead of 5 and all of those indexing issues go away.

Thank you so much! It finally worked the way I wanted. ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.