Calculate Percent
May 21, 2012 at 3:25pm UTC
Something is wrong but can't figure out what..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
//Purpose: to calculate the percent of registered voters who voted in each
// precinct in an election district, which is a collection of precincts
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//function prototypes
double calcPercent(int votes, int registered);
int main()
{
ifstream in_stream ;
int k = 0;
int votes = 0;
int registered = 0;
int numPrecincts = 0;
double percentVote = 0;
string fileName ;
//prompt user to input file name
cout << "Enter file name: " << endl;
cin >> fileName;
in_stream.open(fileName.c_str());
if (in_stream.is_open())
{
in_stream >> numPrecincts;
cout << fixed << setprecision(1) << endl;
cout << setw(8) << "votes" << " " << "registered" << " " << "Prcnt" << endl;
cout << "----------------------------" << endl;
//read numbers
for (k = 0; k < numPrecincts; k++)
{
in_stream >> votes;
in_stream >> registered;
//calculate percent
percentVote = calcPercent(votes, registered);
cout << setw(8) << registered << " " << votes << " " << percentVote << endl;
}
in_stream.close();
//calculate vote percentage
percentVote = calcPercent(votes, registered);
}
else
{
cout << "Cannot open file " << "<" << fileName << ">" << endl;
}
system("pause" );
return 0;
}
double calcPercent(int votes, int registered)
{
double percentVote;
percentVote = (static_cast <double >(registered) / static_cast <double >(votes)) * 100;
return percentVote;
}
Second part:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//function prototype
double calcPercent(int num, int denom);
void printDistrictTotals(int sumVotesCast, int sumRegisteredVoters);
int main ()
{
//variable declaration
int numPrecincts; //number of voting precints
int registeredVoters[10]; //registered votes in precint
int votesCast[10]; //number of votes cast
double votingPercent; //percent of voters casting a vote
ifstream in_stream; //input file object
int sumVotesCast = 0; //sum of votes cast for all precincts
int sumRegisteredVoters = 0; //sum of votes cast for all precincts
double sumVotingPercent; //percent for all precincts
string fileName; //file name
//print options
cout << fixed << showpoint << setprecision(1);
//prompt the user to enter the file name with voting data
//if the file can not be opened, print a message and
//prompt the user for another file name.
cout << "Enter file name: " ;
getline(cin, fileName);
in_stream.open(fileName.c_str());
while (!in_stream.is_open())
{
cout << "Can not open file <" << fileName << ">" << endl;
//reset file status bits
in_stream.clear();
cout << "Enter file name: " ;
getline(cin, fileName);
in_stream.open(fileName.c_str());
}
//read number of voting precints
in_stream >> numPrecincts;
//print table headings
cout << endl;
cout << " 1 2 3" << endl;
cout << "1234567890123456789012345678901234567" << endl;
cout << " votes registered Prcnt rating" << endl;
cout << "-------------------------------------" << endl;
//read all voter information
for (int k=0; k<numPrecincts; k++)
{
in_stream >> registeredVoters[k];
in_stream >> votesCast[k];
}
//compute district totlals
for (int k=0; k<numPrecincts; k++)
{
sumVotesCast += votesCast[k];
sumRegisteredVoters += registeredVoters[k];
}
sumVotingPercent = calcPercent(sumVotesCast, sumRegisteredVoters);
//print precint values
//compute percent voting
for (int k=0; k<numPrecincts; k++)
{
votingPercent = calcPercent(votesCast[k], registeredVoters[k]);
//print results
cout << setw(8) << votesCast[k]
<< setw(12) << registeredVoters[k]
<< setw(8) << votingPercent;
//print rating
if (votingPercent > sumVotingPercent+7.0)
cout << " Excellent" ;
else if (votingPercent < sumVotingPercent-7.0)
cout << " Poor" ;
cout << endl;
}
//print overall statistice
printDistrictTotals(sumVotesCast, sumRegisteredVoters);
//freeze console window and halt
cout << endl;
system("Pause" );
return 0;
} //end main
//calculate percentage
double calcPercent(int num, int denom)
{
double percent = static_cast <double >(num) / static_cast <double >(denom) * 100.0;
return percent;
}
//print district totals
void printDistrictTotals(int sumVotesCast, int sumRegisteredVoters)
{
cout << endl;
cout << setw(8) << sumVotesCast
<< setw(12) << sumRegisteredVoters
<< setw(8) << calcPercent(sumVotesCast, sumRegisteredVoters)
<< " District Total" << endl;
}
Last edited on May 21, 2012 at 3:53pm UTC
May 21, 2012 at 3:30pm UTC
It's not very well formatted.
If you think something else is wrong as well, perhaps you should tell us what it does that you think it shouldn't, or what it shouldn't do that it does. We're not psychic.
May 21, 2012 at 3:34pm UTC
what is the message appeared in the output debugging ??!
ummmm the last step in the calcPercent(int votes , int registerd) I couldnt understand it ..!!
May 21, 2012 at 3:39pm UTC
what is output debugging?
May 21, 2012 at 3:52pm UTC
Just looking at calcPercent(int, int), I think you have the calculation backwards. Should probably be votes / registered (assuming registered is the total)
Topic archived. No new replies allowed.