int and double help?

Trying to print out the "totalVotes" as an int and the "votePerc" as a double. My issue is if I keep their types seperate the "votePerc" calculation comes out to 0.00. I can change the "totalVotes" to a double then it'll work but I need the it printed out as an int. Any help?

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
  void outputVotes(const int table[], int tableSize)
{
    ifstream in;
    in.open ("ballot.txt");
    
    int totalVotes = 0;
    
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        totalVotes = totalVotes + table[i];
    }
    
    double votePerc;
    
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        votePerc = (table[i] / totalVotes) * 100;
    }
    
    for(int i = 0; i < tableSize; i++)
    {
        in >> cand;
        cout << left << setw(10) << cand
        << right << "   " << setw(10) << table[i] << "   " << setw(15) << votePerc << endl;

    }
    
    cout << left << setw(10) << "Total" << right << "   " << setw(10) << totalVotes << endl; 
}
Last edited on
1
2
3
4
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        votePerc = (table[i] / totalVotes) * 100;
    }
You're overwriting the value of votePerc each time in the loop.

This is logically equivalent to:
1
2
if (ARRAY_SIZE > 0)
    votePerc = (table[ARRAY_SIZE - 1] / totalVotes) * 100;


So I am assuming the the last element of your table array is 0.0.

It looks like you want to move line 17 into your last for loop, and delete lines 15 to 18.
Last edited on
Topic archived. No new replies allowed.