c++ candidate program?

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 pgoram should then output each candidate's name, the number of votes received, and the percentage of the total voates recieved by the candidate. The program should also output the winner of the election. here is my code i tried to brute force my way through it but i count figure out how to output the winner. I would like to see this program done with an array if possible!

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

int main()

{

string name1, name2, name3, name4, name5;

double votes1, votes2, votes3, votes4, votes5, totalvotes;

double percent1, percent2, percent3, percent4, percent5;

cout<<"Please Enter Last Name of 5 Candidates:\n "<<endl;

cin>>name1>>name2>>name3>>name4>>name5;

cout<<endl;

cout<<"Please Enter the Votes Received by each Candidate: "<<endl;

cin>>votes1>>votes2>>votes3>>votes4>>votes5;

cout<<endl;

totalvotes = (votes1+votes2+votes3+votes4+votes5);

percent1 = (votes1/totalvotes)*100;

percent2 = (votes2/totalvotes)*100;

percent3 = (votes3/totalvotes)*100;

percent4 = (votes4/totalvotes)*100;

percent5 = (votes5/totalvotes)*100;

cout<<"Candidate"<<" "<<" Votes Received "<<" "<<" % of Total Votes"<<endl<<endl;

cout<<noshowpoint<<setprecision(0)<<name1<<setw(22)<<votes1<<setw(25)<<fixed<<showpoint<<setprecision(2)<<percent1<<endl;

cout<<noshowpoint<<setprecision(0)<<name2<<setw(22)<<votes2<<setw(25)<<fixed<<showpoint<<setprecision(2)<<percent2<<endl;

cout<<noshowpoint<<setprecision(0)<<name3<<setw(22)<<votes3<<setw(25)<<fixed<<showpoint<<setprecision(2)<<percent3<<endl;

cout<<noshowpoint<<setprecision(0)<<name4<<setw(22)<<votes4<<setw(25)<<fixed<<showpoint<<setprecision(2)<<percent4<<endl;

cout<<noshowpoint<<setprecision(0)<<name5<<setw(22)<<votes5<<setw(25)<<fixed<<showpoint<<setprecision(2)<<percent5<<endl;

cout<<noshowpoint<<setprecision(0)<<"Total"<<setw(19)<<totalvotes<<endl;

return 0;

Comments

  • #include <iostream>

    #include <map>

    #include <string>

    using namespace std;

    int main()

    {

    map<string, int> candidate;

    // For temp input

    string Name;

    int Votes;

    int TotalVotes=0;

    cout<<"Enter the names and votes of the candidates: "<<endl;

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

    cin>> Name >> Votes;

    candidate[Name] = Votes;

    TotalVotes += Votes;

    }

    cout<<TotalVotes ;

    map<string, int>::const_iterator beginz = candidate.begin();

    map<string, int>::const_iterator end = candidate.end();

    while ( beginz != end ) {

    cout<< beginz->first <<" has Recieved "<< beginz->second <<" votes" <<" , Total percentage of votes are :" << ((double(beginz->second)/TotalVotes)*100) <<endl;

    ++beginz;

    }

    return 0;

    }

  • try this out

    #include <string>

    #include<iostream>

    using namespace std;

    class ElectionResults

    {

    public:

    string name;

    int votes;

    };

    int main()

    {

    ElectionResults res[5];

    int voteTotal = 0;

    int winner = 0;

    int byVotes = 0;

    for(int i = 0; i < 5; i++)

    {

    cout << "Please Enter Last Name of Candidate " << i+1 << ": ";

    cin >> res[i].name;

    cout<<"Please Enter the Votes Received for Candidate " << i+1 << ": ";

    cin >> res[i].votes;

    voteTotal += res[i].votes;

    if( res[i].votes > byVotes)

    winner = i;

    }

    for(int i = 0; i < 5; i++)

    {

    cout << "Candidate: " << res[i].name <<" votes: " << res[i].votes << "percent :" <<(res[i].votes*100)/voteTotal << "%." <<endl;

    }

    cout << "Winner = " << res[winner].name<<endl;

    cin >> voteTotal;

    }

  • You have to create a function that compares the votes with each other. This will get you started

    http://en.wikipedia.org/wiki/Vector_(C%2B%2B)

Sign In or Register to comment.