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?

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.2: Returning A Single Value
Problem 13E
icon
Related questions
Question

#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?
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr