Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ 1. Write a program that allows the user to enter the last names offive candi

ID: 3804230 • Letter: C

Question

C++

1. Write a program that allows the user to enter the last names offive candidates in a local election and the votes received by each candidate. The program should then output each candidate’s name, votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is as follows:
Candidate Johnson Miller
Duffy
Robinson Sam
Total
Votes Received 5000 4000 6000 2500 1800
19300
The Winner of the Election is Duffy.

#include <iostream>
#include <string>

using namespace std;
int main()
{
int winIndex = 0;
int max = 0;
int total=0;
string candiName[5];
int candiVotes[5];
for (int i = 0; i<5; i++) {
  cout << "Enter Candicate " << (i + 1) << " Name : ";
  cin >> candiName[i];
  cout << "Enter Candicate " << (i + 1) << " Votes : ";
  cin >> candiVotes[i];
  cout << " ";
  total += candiVotes[i];
  if (max<candiVotes[i]) {
   max = candiVotes[i];
   winIndex = i;
  }
}
cout << "Total Votes Received by all: " << total << endl;
cout << "The Winner of the Election is " << candiName[winIndex] << endl;
system("pause");
return 0;
}

NOW THIS IS MY QUESTION

can you please Modify this solution

1. reading an arbitrary number of names and votes from a file

2. recognizing more than 1 winning candidate - several candidates with the greatest number of votes

Explanation / Answer

Solution of first ques:-

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>

using namespace std;
class bubble
{
public:
//candiVotesay of integers to hold values
int* candiVotes;

//Number of elements in candiVotesay
int n;

//Constructor
bubble(const int& size) : n(size) { this->candiVotes = new int[n]; }

//function to read from file
void inputvf(istream &f)
{
//check if file is open

if (f.fail()) {
cout << " Error in openning file!!!";
exit(1);
}
for (int i = 0; i < n; i++)

f >> candiVotes[i];
//delete[] candiVotes;
//close file
//f.close();
}

//Bubble sort function
void bubblesort()
{
for (int i = 1; i<n; i++)//for n-1 passes
{
for (int j = 0; j<n - 1; j++)
{
if (candiVotes[j] < candiVotes[j + 1])
{
int temp;
temp = candiVotes[j];
candiVotes[j] = candiVotes[j + 1];
candiVotes[j + 1] = temp;
}
}
}
}
void display()
{
cout << endl;
cout << "---------------------- ";
cout << "Sorted candiVotesay elements ";
cout << "---------------------- ";
if (candiVotes >= 0){
for (int j = 0; j < n; j++)
cout << candiVotes[j] << endl;
}
}
};


int _tmain(int argc, _TCHAR* argv[])
{
bubble list(7);
ifstream file("Text.txt");
list.inputvf(file);
list.bubblesort();
list.display();

_getch();
return 0;
}