Question: From this comment---- Indicate how useful the feedback was. Did you understand the comments/observations/suggestions? Did the review point out flaws/shortcomings in your program? Did you learn anything from the critique?
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//Declaring the function
void sum_of_votes();
//Defining the main
int main() {
sum_of_votes();
return 0;
}
void sum_of_votes()
{
int Max=10;
ifstream inputFile;
inputFile.open("ballot.txt");
//Variable Initialzing
string candidates[Max];
int votes[Max];
float percent[Max];
int max=0,total_votes=0;
int i=0,count=0;
string name;
//Looping
while (!inputFile.eof())
{
inputFile>>candidates[i];
cout<<"Enter the number of votes received by "<<candidates[i]<<" :";
count+=1;
//Getting the candidate name and votes
cin>>votes[i];
cout<<endl;
if(votes[i]<0)
{
cout<<"The number of votes cannot be less than 0. Please try again"<<endl;
cout<<"Enter the number of votes received by "<<candidates[i]<<" :";
cin>>votes[i];
cout<<endl;
}
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout<<"This is not a number! Please try again."<<endl;
cout<<"Enter the number of votes received by "<<candidates[i]<<" :";
cin>>votes[i];
cout<<endl;
}
//calculating the total votes
total_votes+=votes[i];
i+=1;
}
//Looping
for(int i=0;i<count;i++)
{
percent[i]=(votes[i]/(float)total_votes)*100.0;
if(percent[i]>percent[max])
{
//Storing the max value
max=i; }
}
\
cout<<endl;
cout<<"Candidate"<<"\t\t"<<"Votes Received"<<"\t\t\t"<<"% of Total Votes"<<endl;
for(int i=0;i<count;i++)
{
std::cout << std::left << std::setw(30) << candidates[i] << std::setw(30) << votes[i] <<std::setw(30)<<fixed<<setprecision(2)<<percent[i] <<'\n';
}
cout<<std::left<<setw(30)<<"Total Votes"<<setw(30)<<total_votes<<endl;
cout<<"The winner of the Election is "<<candidates[max]<<".\n";
}
Comment: I really like the design of your code as it is really simple and easy to read. I can follow through line by line and I don't really need the comments to need to know what is happening where, it's simply put. The documentation is well placed and isn't overly done. There is documentation at nearly every step that needs it, which is helpful for reference. Overall, to me the code looks fairly efficient as every variable is used and there isn't any unnecessary lines of code. Nice work!
Question: From this comment----
- Indicate how useful the feedback was.
- Did you understand the comments/observations/suggestions?
- Did the review point out flaws/shortcomings in your
program ? - Did you learn anything from the critique?
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)