I'm creating a voting program in which you input the persons name and how many votes they have received. It than prints out the persons name, their amount of votes and the percentage of their votes from the total.

I can't seem to get the percent of total to work correctly, everything else is working fine. I'm also aware that nothing lines up correctly, i'll be fixing that after I get it working.

Any ideas? thanks.

#include <iostream>
#include <iomanip>
#include <String>
using namespace std;

int sumVotes(int list[]);  
int winnerIndex(int list[]); 
int votes[20];
double percent[10];

int main()
{
	string name[10];
	
	int i;
	for(i=0; i<5; i++)
	{
		cout << "Enter the name of the candidate and the number of votes: ";
		cin >> name[i];
		cin >> votes[i];
	}
	
	int totalvote;
	totalvote = sumVotes(votes);
	
	int w;
	w = winnerIndex(votes);

	cout << "Name" << setw(25) << "Votes Receieved" << setw(25) << "% of Total" << endl;
	for(i=0; i<5; i++)
	{
		percent[i] = totalvote;
		cout << name[i] << setw(21) << votes[i] << setw(20) << percent[i] << endl;
	}

	cout << endl << setw(30) << "Total: " << totalvote;

	return 0;
}
int sumVotes(int votes[])
{
	int i;
	int total = 0;
	for(i=0; i<5; i++)
		total = total + votes[i];

	return total;
}
int winnerIndex(int votes[])
{
	int i;
	int maximum;
	maximum = votes[0];
	for(i=0; i<5; i++)
	{
		if(votes[i] > maximum)
			maximum = votes[i];
	}

	return maximum;
}

Output:

Enter the name of the candidate and the number of votes: Person1 5000
Enter the name of the candidate and the number of votes: Person2 4000
Enter the name of the candidate and the number of votes: Person3 6000
Enter the name of the candidate and the number of votes: Person4 2500
Enter the name of the candidate and the number of votes: Person5 1800
Name          Votes Receieved               % of Total
Person1                 5000                   0
Person2                 4000                   0
Person3                 6000                   0
Person4                 2500                   0
Person5                 1800                   0

Edited 5 Years Ago by BTW8892: n/a

percent = totalvote;

that isn't correct.

Try making it a percentage based on totalvote and the number of votes they actually got.

percent = totalvote;

that isn't correct.

Try making it a percentage based on totalvote and the number of votes they actually got.

Sorry, that was an error, I tried testing to see what was wrong with it before. Here is the actual line that outputs 0.

percent[i] = (votes[i] / totalvote) * 100;

before solving your problem lets look at the following code snippet

int x=4;
int y=8;
float z=x/y;
cout<<z;

Can you try this code and see what output you get.

You might be expecting the output to be: 0.5 right ?

But have a second look at the code. You are dividing an integer by another integer, so the result will also be an integer. Hence the output of the above code snippet will be:
0
instead of 0.5.

I hope you got the problem with your code.
If you want to get the exact value after division, you will have to typecast at-least the number you are dividing or the divisor to float type.

Hence the above code snippet should be corrected to

int x=4;
int y=8;
float z=((float)x)/y;/*or float z=(float)x/y;*/
cout<<z;

before solving your problem lets look at the following code snippet

int x=4;
int y=8;
float z=x/y;
cout<<z;

Can you try this code and see what output you get.

You might be expecting the output to be: 0.5 right ?

But have a second look at the code. You are dividing an integer by another integer, so the result will also be an integer. Hence the output of the above code snippet will be:
0
instead of 0.5.

I hope you got the problem with your code.
If you want to get the exact value after division, you will have to typecast at-least the number you are dividing or the divisor to float type.

Hence the above code snippet should be corrected to

int x=4;
int y=8;
float z=((float)x)/y;/*or float z=(float)x/y;*/
cout<<z;

Thank you so much!

I can't believe I overlooked that, stupid mistake on my part haha.

Thank you so much!

I can't believe I overlooked that, stupid mistake on my part haha.

I just had one more question, how would I got about getting the name of the winner with the most votes?

You almost have the code for that...
You have written:

int winnerIndex(int votes[])
{
   int i;
   int maximum;
   maximum = votes[0];
   for(i=0; i<5; i++)
     {
    if(votes[i] > maximum)
      maximum = votes[i];
     }
  return maximum;
}

Now if you add something like this:

int winnerIndex(int votes[])
   // This function now returns the index in votes that is the maximum:
{
   int index(0);
   int maximum(votes[0]);
   for(int i=1; i<5; i++)
     {
    if(votes[i] > maximum)
          {
        maximum = votes[i];
            index=i;
          }
     }
  return index;
}

Note I have changed a few thing:

(a) you don't need to loop through the i=0 index because you have effectively processed that in the initialization stage.
(b) I have elected to maintain two variables, maximum and index, you could do it with
just the index value but each comparison would need to be to votes[index].
(c) There is not code to deal with a tie.

You might like to change this function to include an extra input parameter to the size of the array, at the moment it is hard coded to 5, that is really ugly. And a silent bug waiting to happen when you add/remove a candidate.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You almost have the code for that...
You have written:

int winnerIndex(int votes[])
{
   int i;
   int maximum;
   maximum = votes[0];
   for(i=0; i<5; i++)
     {
    if(votes[i] > maximum)
      maximum = votes[i];
     }
  return maximum;
}

Now if you add something like this:

int winnerIndex(int votes[])
   // This function now returns the index in votes that is the maximum:
{
   int index(0);
   int maximum(votes[0]);
   for(int i=1; i<5; i++)
     {
    if(votes[i] > maximum)
          {
        maximum = votes[i];
            index=i;
          }
     }
  return index;
}

Note I have changed a few thing:

(a) you don't need to loop through the i=0 index because you have effectively processed that in the initialization stage.
(b) I have elected to maintain two variables, maximum and index, you could do it with
just the index value but each comparison would need to be to votes[index].
(c) There is not code to deal with a tie.

You might like to change this function to include an extra input parameter to the size of the array, at the moment it is hard coded to 5, that is really ugly. And a silent bug waiting to happen when you add/remove a candidate.

Thanks man, I finally got it working how I needed it to work. Much appreciated with the tips at the bottom, they really helped.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.