OK, I have been out of the loop with C++ for a year now. I am finished with school, so this is NOT homework. Just trying to clean up some old assignments for future references.
(the code is in "messy" shape til I can get it working properly and clean it up. that's why there are so many cout statements for checking what the hell is going on).
Running the program you see it asks for number of voters, and their names, and how many votes were received. That parts all good. When I do
perc = votes[i] /2;
or
perc = votes[i] * 100;
it works properly. But the
perc = (votes[i] / sum) *100;
to get the percentage, it's failing out. Like I said, probably an easy fix, just not seeing it. Again, out of the loop. Had java II, PHP, and ASP.NET since then and it's killing me that this is so simple and I'm not seeing it. So any advice is GREATLY appreciated.
the code:
//This program Dynamicly allocates memory for user to input number of array members for input
//data of an election and outputs names, number of votes, percentage, total, and winner.
//
//10-19-2010
#include <iostream>
#include <string>
using namespace std;
int main()
{
string *name, // pointer to name array
winner;
int *votes; // pointer to vote array
int i,
largest,
sum = 0,
*perc,
total= 0,
numVotes;
cout << "How many voters are there?: ";
cin >> numVotes;
cout<<endl;
cin.ignore();
name = new string[numVotes]; // Dynamicly allocate names
for(i=0;i < numVotes; i++){
cout<<"Enter canidates last names: ";
getline(cin,name[i]);
}
perc = new int[numVotes]; // Dynamicly allocated percents
votes = new int[numVotes]; // Dynamicly allocate votes
cout<<endl;
for(i = 0;i < numVotes; i++){ // loop for biggest vote
cout << "Enter votes received for "<< name[i] << ": ";
cin >> votes[i];
total = total + votes[i];
}
largest = votes[0];
winner = name[0];
for(i=0;i<numVotes;i++){
if(votes[i]>largest){
largest = votes[i];
winner = name[i];
}
} //end loop for biggest vote/winner
cout<<endl;
cout << "Canidate\t" << "Votes Received " << "\t% of Total Votes"<<endl;
for(i=0;i < numVotes; i++){
total = votes[i]; // get total
sum = sum + total;
//perc[i] = (votes[i]/ sum)* 100;
//cout << "percent is: " << perc[i] << " : voting numbers are " << votes[i] << " " << " : the sum is " << sum << endl;
// percent of votes entered by user (by total, number of votes, and used 100%)
/*cout << fixed << showpoint;
cout << name[i] << " \t " << votes[i] << " \t " << setprecision(2) << perc << endl;*/
}
/*------------------------------------AREA OF CONCERN------------------------------------------------------------------*/
for(i=0;i < numVotes; i++){
cout <<"sums are: " << " " << sum;
perc[i] = (votes[i] / sum) * 100; // <-----Getting "0" as perc. tried just varaible as in "perc" as int and same output.
cout << "percent is: " << perc[i] << " : voting numbers are " << votes[i] << " " << " : the sum is " << sum << endl;
}
/*--------------------------------------END CONCERN---------------------------------------------------------------------*/
cout<<"\nTotal: \t\t" << sum <<endl;
cout <<"\nThe winner of the Election is " << winner << "."<< endl;
delete [] votes; // delete vote array out of memory
delete [] name; // delete name array out of memory
delete [] perc;
cin.sync();
cin.peek();
}