Help with output statement

closed account (z8q4izwU)
Hello, i need help with an output. The program calculates votes for an election and the percent of each candidate and it suppose to output a winner. But i cant figure it out. If anyone can give some ideas on how to do this id appreciate it.

Thanks Gary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <iomanip>
#include <String>

using namespace std;
// Declaring the functions

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

int main()
{
	string name[5];
	
	int i;
	int winner;
	
	
	cout << fixed << showpoint;
    cout << setprecision(2);
    
    cout << "Please enter the five candidates running and the amount of votes recieved. \n";
 
	for(i=0; i<5; i++)// Loop to get all candidate and votes
	{
		cout << "\nEnter the name of the candidate and the number of votes: \n";
		cin >> name[i];
		cin >> votes[i];
		
	}
	//Declaring 
	int totalvote;
	totalvote = sumVotes(votes);

	
	int w;
	w = winnerIndex(votes);
//Prints Results

	cout << "\nName" << setw(25) << "Votes Receieved" << setw(20) << "% of Total \n" << endl; 
	for(i=0; i<5; i++)
	{
		percent[i] = static_cast<double>(votes[i]) / static_cast<double> (totalvote) * 100;

		cout << name[i] << setw(21) << votes[i] << setw(20) << percent[i] << endl;
	}

	cout << endl << setw(0) << "Total Votes: " << totalvote;
	
	cout << "\nThe winner is: " << winner << endl;
	
	cout << "\n" << endl;

	system ("pause");

	return 0;
}

int sumVotes(int votes[])//Array to add votes together
{
	int i;
	int total = 0;
	for(i=0; i<5; i++)
		total = total + votes[i];

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

	return maximum;
}
What is the problem exactly ?
closed account (z8q4izwU)
It should out put the winners name but it doesnt, it prints out a number and cant figure out how to make it print the name.
Last edited on
On line 79, you are assigning i to winner, which is local to winnerIndex(). Why? Nothing is done with winner after that since you return maximum and winner is local only to the function so is destroyed once the function exits. Also, the winner variable declared in main() is of type int. It can only contain a number. Why wouldn't it output a number?
closed account (z8q4izwU)
I think i declared it in the mainline so it would compile. How should i go about on fixing this? As in making it print the name of the winner?
closed account (z8q4izwU)
My teacher wants the name, at one point i had it running and it output the higher number but cant figure out how i did that either.
In winnerIndex() you should return the winner index, ie "winner".
In your main(), the name of the winner will then be name[w]
closed account (z8q4izwU)
Thanks that worked!!!!
Topic archived. No new replies allowed.