# Help with looping problem

Page 1 of 1

## 2 Replies - 739 Views - Last Post: 19 December 2010 - 02:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=206129&amp;s=ee4e5cfd1127f05e291a70154eff4d6f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chasethesun91

Reputation: 0
• Posts: 2
• Joined: 19-December 10

# Help with looping problem

Posted 19 December 2010 - 01:36 PM

Okay, hello, my code works all most according to the standards, but i can't seem to figure out how to get the total out of the loop and insert it into another loop. Basically, I can't get the percentage of the votes from each candidate because the total stays as 0 in the loop. I've been at this for days & can't figure it out. Please help asap, anyways, here is the code:

```#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()

{
string name[10];
int total = 0;
double percent[10];
int m, i, j, sum = 0, max = 0;

cout << "Please enter 5 candidates followed by the votes they recieved here:\n";

for(i = 0; i < 5; i++)
{
cin >> name[i];
}

for (j = 0; j < 5; j++)
percent[j] = votes[j]/ total * 100;

cout << left << setw(10) << "Canditates" << right << setw(15) << "Votes" << setw(12) << "Percent" << endl;

cout << setprecision(2);

for(i=0; i <5; i++)
cout << left << setw(10) << name[i] << right << setw(15) << votes[i] << setw(12) << percent[i] << endl;

cout << left << setw(10) << "Total:" << right << setw(15) << total << endl;

m = 0;

for (i = 0; i < 5; i++)
{
{
max = i;
}

}

cout << "The winner of the election is: " << name[max] << endl;

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Help with looping problem

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3802
• Posts: 13,797
• Joined: 08-August 08

## Re: Help with looping problem

Posted 19 December 2010 - 01:47 PM

votes[], total, and 100 are all of type int, so the answer will be of type int.
Try this:
```	for (j = 0; j < 5; j++)
percent[j] = (double)votes[j]/ total * 100;

```

### #3 chasethesun91

Reputation: 0
• Posts: 2
• Joined: 19-December 10

## Re: Help with looping problem

Posted 19 December 2010 - 02:01 PM

Thanks so much! it really helped, all i needed was an extra showpoint @ the end and it came out exactly how it was supposed to. thanks again!