Help with looping problem

I need to figure out how to get info out of a loop.

Page 1 of 1

2 Replies - 707 Views - Last Post: 19 December 2010 - 02:01 PM Rate Topic: -----

#1 chasethesun91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 votes[20];
	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];
		cin >> votes[i];
		total = total + votes[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++)
	{
		if (votes[i] > m)
		{
			m = votes[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  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3784
  • View blog
  • Posts: 13,714
  • 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;


Was This Post Helpful? 2
  • +
  • -

#3 chasethesun91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1