This is my assigment and i have no idea to do that. Somebody please help !


Write a program that reads from a file the number of candidates in a local election,
then their last names and the number of votes received by each.

Use dynamic memory allocation to store the information in one or more arrays.

The program will then output each candidate’s name, the number of votes received,
and the percentage (use 2 decimals of precision) of the total votes received by the candidate.
(The total of percentages may not add up to 100 because of rounding errors)

Use a minimum of 2 functions (in addition to main).
At least one of those functions must pass an array and its size as parameters.

Your program will also output the winner of the election.

A sample file is:

5
Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Ashtony 1800

The sample output for that data 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.

Edited 6 Years Ago by hking: n/a

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct Person
{
       string first;
       string votes;
    
};
int main ()
{
   

    //read name into dymanically allocated array
    ifstream names("names.txt");
    if( ! names ) { cout << "error reading names"; exit(1); }
    int count = 0;
    names >> count;
    cout << count;
    Person* p = new Person [count]; 
    for ( int i = 0; i < count; i++ )
    {
        names >> p[i].first;
        names >> p[i].votes;
    }
    for ( int i = 0; i < count; i++ )
    {
        cout << p[i].first << ' ';
        cout << p[i].votes << ' ';
        
    }

i done the reading names part but i dont know what to do next ! Please help !

Edited 6 Years Ago by WaltP: Added CODE Tags

Let's see -- you've read the names and you've read the votes. What do your instructions say to do next?

@coolzinthis this is pretty straight-forward:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct Person {
  string first;
  int votes;
};

int computeTotalVotes(Person* candidates, int count) {
  if(candidates == NULL) return 0;
  int sum = 0;
  for(int i = 0; i < count; i++)
    sum += candidates[i].votes;
  return sum;
};

void printCandidateResults(Person* candidates, int count, int totalVotes) {
  cout << "Candidate Votes Received % of Total Votes" << endl;
  cout << "---------------------------------" << endl;
  Person winner;
  winner.votes = 0;
  for(int i = 0; i < count; i++) {
    cout << candidates[i].first << " " 
         << candidates[i].votes << " " 
         << 100.0 * ((float)candidates[i].votes) / totalVotes << endl;
    if(winner.votes < candidates[i].votes)
      winner = candidates[i];
  };
  cout << "---------------------------------" << endl;
  cout << "Total " << totalVotes << endl;
  cout << "The Winner of the Election is " << winner.first << "." << endl;
};

int main () {
  //read name into dymanically allocated array
  ifstream names("names.txt");
  if( ! names ) { cout << "error reading names"; exit(1); }
  int count = 0;
  names >> count;
  Person* p = new Person [count];
  for ( int i = 0; i < count; i++ ) {
    names >> p[i].first;
    names >> p[i].votes;
  };

  int total = computeTotalVotes(p,count);
  printCandidateResults(p,count,total);

  delete p;
  return 1;
};
Comments
Don't reward irrelevant bumps. Especially by not following the posting guidelines.

How would one set this up so that the user can input the file name themselves instead of being restricted to "names.txt"?

This is how:

...
int main (int argc, char** argv) {
  //read name into dymanically allocated array
  string filename = "names.txt";
  if(argc > 1)
    filename = argv[1];
  ifstream names(filename);
  if( ! names ) { cout << "error reading names"; exit(1); }
...

You should note though that if this is an assignment for university if you're techniques that you haven't been taught it will look strange and probably be done for plagerism.

Your better off submitting poor work of your own that someone elses work.

@Mike_2000_17:

We do not give a out free code to people who show no effort. Although Hkning (the OP) has shown some effort, coolzinthis did not and is just here for a homework-freebie. Please keep in mind that you can teach people more by telling them how they should do it themselves, rather then doing it for them. I appreciate your enthusiasm however, so keep posting!

Edited 6 Years Ago by Nick Evan: typo

Point taken! I agree very much, I just sometimes feel a few lines of code can explain better than a whole bunch of writing. It's just that C++ is a more natural language for me to explain stuff than English.

I'll be more diligent about that in the future.

This article has been dead for over six months. Start a new discussion instead.