LLDB error in XCode program! Help!

This is the program I'm currently working on:
Write a program that allows the user to enter the last names of five candidates
in a local election and the number of votes received by each candidate. The
program should then output each candidate’s name, the number of votes
received, and the percentage of the total votes received by the candidate.
Your program should also output the winner of the election. A sample
output is:
Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.73
Duffy 6000 31.09
Robinson 2500 12.95
Ashtony 1800 9.33
Total 19300
The Winner of the Election is Duffy.

**** My program was partially running before and just output Ashtony as the winner which isn't true. I went back into the function theWinner and then I got an lldb error and I don't know what that is. Also How would I be able to output Duffy as the winner?

HERE IS MY CODE

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int theWinner(int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);

const int candidateNumber = 5;

int main()
{  
    string candidates[candidateNumber];
    int votes[candidateNumber];
    cout << "Input the 5 candidates and their number of votes: ";
    for (int i = 0; i < candidateNumber; i++) {
        cin >> candidates[i] >> votes[i];
    }
    printResults(candidates, votes);
    cout << "The winner of the Election is " << candidates[theWinner(votes)] << endl;
    return 0;
}
double calculatePercentage(int votes[], int vote){
    int sumOfVotes = 0;
    for (int i = 0; i < candidateNumber; i++) {
        sumOfVotes += votes[i];
    }
    double percentage = static_cast<double>(vote) / sumOfVotes;
    return percentage*100;
}
void printResults(string candidates[], int votes[]){
    cout << "Candidate" << setw(15) << "Votes Received" << setw(15) << "% of Total Votes" << endl;
    for (int i = 0; i < candidateNumber; i++) {
        cout << candidates[i] << setw(15) << votes[i] << setw(15);
        int percentage = calculatePercentage(votes, votes[i]);
        cout << percentage << "%" << endl;       
    }
}
int theWinner(int votes[]){
    int winner = 0;
    for (int i = 0; i < candidateNumber; i++) {
        if (votes[i] > winner)
            winner = votes[i];
    }
    return winner;
}
Hi,

Set winner to equal the first value in the array, before the loop starts. At the moment line 43 is always true and it shows the winner as being the last value in the array. Would it be better to return i , then you could use that to look up the candidates name?

Good Luck !!

Edit>
What was the entire text of the error message ?
Last edited on
Topic archived. No new replies allowed.