Vector Program Help

I need help with a c++ program using vectors. Below is my code

It requires me to use vectors.
This is the question:

Write a programme 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 programme should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your programme 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
Ashton 1800 9.33

Total 19300


I am using Code BLocks IDE,
This 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include <iostream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

int findWinner( vector<int> votes[]);
void printResults( vector<string> candidates[], vector <int> votes[]);
double calculatePercentage( vector<int> votes[], int vote);

const int NUMBER_OF_CANDIDATES = 5;

int main()
{

    vector<string> candidates[NUMBER_OF_CANDIDATES];
    vector<int> votes[NUMBER_OF_CANDIDATES];

    cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";
    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        cin >> candidates[i] >> votes[i];
    }
    
    printResults(candidates, votes);
    cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
    return 0;
}

double calculatePercentage( vector <int> votes[], int vote){
    int sumOfVotes = 0;

    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        sumOfVotes += votes[i];
    }

    double percentage = static_cast<double>(vote) / sumOfVotes;

    return percentage*100;
}


void printResults( vector<string> candidates[], vector<int> votes[]){
    cout << "Name:" << setw(15) << "Votes:" << setw(15) << "Percentage:" << endl;
    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        cout << candidates[i] << setw(15) << votes[i] << setw(15);
        int percentage = calculatePercentage(votes, votes[i]);
        cout << percentage << "%" << endl;

    }
}

int findWinner( vector<int> votes[]){
    int winner = 0;
    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        if (votes[i] > winner)
            winner = i;
    }
    return winner;
}


However, there is an error on line 22 saying: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘std::vector<std::__cxx11::basic_string<char> >’)
cin >> candidates[i] >> votes[i].

I would appreciate some help. THanks
Last edited on
Vectors are NOT arrays.

To instantiate a vector with a known size:
vector<int> votes(NUMBER_OF_CANDIDATES);

To pass a vector into a function use a reference. If the vector should not be changed, a const reference:
int findWinner(const vector<int>& votes);

Vectors passed into function retain their size, unlike arrays. For loops should use a vector's size:
53
54
55
56
57
58
59
60
61
62
int findWinner(const vector<int>& votes)
{
   int winner = 0;
   for (int i = 0; i < votes.size(); i++)
   {
      if (votes[i] > winner)
         winner = i;
   }
   return winner;
}


After making the changes the output can be:
Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: able 100
baker 200
delta 500
gamma 300
zed 400
Name:         Votes:    Percentage:
able            100              6%
baker            200             13%
delta            500             33%
gamma            300             20%
zed            400             26%
And the winner is: zed

Clearly your winner calculation is not working, though the percentage calculation is.
Hi, thanks for the code, but the program still not running. THis is the fixed 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

int findWinner( vector<int> votes[]);
void printResults( vector<string> candidates[], vector <int> votes());
double calculatePercentage( vector<int> votes[], int vote);

const int NUMBER_OF_CANDIDATES = 5;

int main()
{

    vector<string> candidates[NUMBER_OF_CANDIDATES];
    vector <int> votes(NUMBER_OF_CANDIDATES);

    cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";

    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {

            cin >> candidates[i]; cin >> votes[i];
    }

    printResults(candidates, votes);
    cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
    return 0;
}

double calculatePercentage( vector <int> votes[], int vote){
    int sumOfVotes = 0;

    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        sumOfVotes += votes[i];
    }

    double percentage = static_cast<double>(vote) / sumOfVotes;

    return percentage*100;
}


void printResults( vector<string> candidates[], vector<int> votes[]){
    cout << "Name:" << setw(15) << "Votes:" << setw(15) << "Percentage:" << endl;
    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        cout << candidates[i] << setw(15) << votes[i] << setw(15);
        int percentage = calculatePercentage(votes, votes[i]);
        cout << percentage << "%" << endl;

    }
}

int findWinner( cont vector<int>& votes[]){
    int winner = 0;
    for (int i = 0; i < votes.size(); i++) {
        if (votes[i] > winner)
            winner = i;
    }
    return winner;
}



What should I do then?
Last edited on
You didn't correct all the "vector as array" problems:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

int findWinner(const vector<int>& votes); // <--- !!!!
void printResults(const vector<string>& candidates, const vector<int>& votes); // <--- !!!!!
double calculatePercentage(const vector<int>& votes, int vote); // <--- !!!!!

int main()
{
   int NUMBER_OF_CANDIDATES = 5; // <--- !!!!!

   vector<string> candidates(NUMBER_OF_CANDIDATES); // <--- !!!!!
   vector <int> votes(NUMBER_OF_CANDIDATES);

   cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";

   for (int i = 0; i < candidates.size(); i++) // <--- !!!!!
   {
      cin >> candidates[i]; cin >> votes[i];
   }

   printResults(candidates, votes);

   cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
}

double calculatePercentage(const vector<int>& votes, int vote) // <--- !!!!!
{
   int sumOfVotes = 0;

   for (int i = 0; i < votes.size(); i++) // <--- !!!!!
   {
      sumOfVotes += votes[i];
   }

   double percentage = static_cast<double>(vote) / sumOfVotes;

   return percentage * 100;
}


void printResults(const vector<string>& candidates, const vector<int>& votes) // <--- !!!!!
{
   cout << "Name:" << setw(15) << "Votes:" << setw(15) << "Percentage:" << endl;

   for (int i = 0; i < candidates.size(); i++) // <--- !!!!!
   {
      cout << candidates[i] << setw(15) << votes[i] << setw(15);
      int percentage = calculatePercentage(votes, votes[i]);
      cout << percentage << "%" << endl;
   }
}

int findWinner(const vector<int>& votes) // <--- !!!!!
{
   int winner = 0;

   for (int i = 0; i < votes.size(); i++)
   {
      if (votes[i] > winner)
         winner = i;
   }
   return winner;
}

Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: alpha 100
beta 200
delta 500
gamma 300
zed 400
Name:         Votes:    Percentage:
alpha            100              6%
beta            200             13%
delta            500             33%
gamma            300             20%
zed            400             26%
And the winner is: zed
There are times when using preset data is beneficial, instead of constantly asking for user input. Change your main to look like this:

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main()
{
   int NUMBER_OF_CANDIDATES = 5; // <--- !!!!!

   //vector<string> candidates(NUMBER_OF_CANDIDATES); // <--- !!!!!
   //vector<int> votes(NUMBER_OF_CANDIDATES);

   vector<string> candidates { "Alpha", "Beta", "Delta", "Gamma", "Zed" };
   vector<int> votes { 100, 200, 500, 300, 400 };

   //cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";

   //for (int i = 0; i < candidates.size(); i++) // <--- !!!!!
   //{
   //   cin >> candidates[i]; cin >> votes[i];
   //}

   printResults(candidates, votes);

   cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
}

You now have 5 pre-defined elements in your candidates and votes vectors every time you run the program. No data entry happens.

Now you can figure out why your code is not giving you the correct winner, without the distraction of having to enter data in each run.

Hint: the winner is Delta with 500 votes, not Zed at 400.

When you get the logic bugs worked out, then remove the two lines that give pre-defined data (lines 19-20), and un-comment the lines currently commented out. Now your program will ask for user input again.

FYI, with vectors you are not restricted to a pre-defined number of elements. A vector can be dynamically resized at run-time. You could have more or less than 5 candidates (and votes). By using a vector's size in the for loops the number of elements will be correctly accounted for at run-time.
Last edited on
Thank you so much for the code, I still learning more about vectors. As a matter of fact, I have been researching only about vectors. I did not think about using default data. I really appreciate your help. :)
Topic archived. No new replies allowed.