right now my program asks for 5 candidates but i want to make the user input as many candidates as they want to. how would i go about doing that??

#include <iomanip>
#include <string>
using namespace std;

int sumVotes(int list[], int size);
int winnerIndex(int list[], int size);
int main()
{
    string candidates[5];
    int votes[5] = {0};
int totalVotes;
int i;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << "Enter candidate's name and the votes received by "
         <<"the candidate." << endl;

    for (i = 0; i < 5; i++)
        cin >> candidates[i] >> votes[i];
    totalVotes = sumVotes(votes, 5);
    cout << "Candidate    Votes Received   % of Total Votes" << endl;
    for (i = 0; i < 5; i++)
        cout << left << setw(10) << candidates[i]
             << right << " " << setw(10) << votes[i] << "   " << setw(15)
             << (static_cast<double>(votes[i]) / 
static_cast<double>(totalVotes)) * 100 
             << endl;
    cout << "Total            " << totalVotes << endl;
    cout << "The Winner of the Election is "
         << candidates[winnerIndex(votes, 5)] 
         << "." << endl;
    return 0;
}
int sumVotes(int list[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
sum = sum + list[i];return sum;
}
int winnerIndex(int list[], int size)
{
    int winInd = 0;
for (int i = 0; i < size; i++)
        if (list[i] > list[winInd])
            winInd = i;
    return winInd;
}

Recommended Answers

All 3 Replies

Try using cin.getline() along with gcount() together where in getline() can fetch you the string and gcount can help you stop according to a particular condition. Use it with caution as getline sets some error flags when encountered with a premature end of input.

right now my program asks for 5 candidates but i want to make the user input as many candidates as they want to. how would i go about doing that??

#include <iomanip>
#include <string>
using namespace std;

int sumVotes(int list[], int size);
int winnerIndex(int list[], int size);
int main()
{
string candidates[5];
int votes[5] = {0};
int totalVotes;
int i;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Enter candidate's name and the votes received by "
<<"the candidate." << endl;

for (i = 0; i < 5; i++)
cin >> candidates >> votes;
totalVotes = sumVotes(votes, 5);
cout << "Candidate Votes Received % of Total Votes" << endl;
for (i = 0; i < 5; i++)
cout << left << setw(10) << candidates
<< right << " " << setw(10) << votes << " " << setw(15)
<< (static_cast<double>(votes) /
static_cast<double>(totalVotes)) * 100
<< endl;
cout << "Total " << totalVotes << endl;
cout << "The Winner of the Election is "
<< candidates[winnerIndex(votes, 5)]
<< "." << endl;
return 0;
}
int sumVotes(int list[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
sum = sum + list;return sum;
}
int winnerIndex(int list[], int size)
{
int winInd = 0;
for (int i = 0; i < size; i++)
if (list > list[winInd])
winInd = i;
return winInd;
}


An example would be:

int number;
cout << "how many entries?" << endl;
cin >> number;
string* dyn_array = new string[number];
for (unsigned int i = 0; i < number; ++i){
  cin >> dyn_array[i];
}

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.