Below is the entire paste of my code, it is so large I had to put it in an additional detail. I seriously do not understand a way around the problem I seem to be having, if you compile the code, you'll see (and yes the error part is marked, which is in the Output function definition...that it doesn't chose the right canidate...well it does but ONLY sometimes! I try it with 5 canidates (for my project requirement), if the VALUE OF canidate 0 in the array is the highest in that array, then it will always pick the last value in the array!
ANY help would be appreated!
2006-10-18
14:23:45
·
4 answers
·
asked by
D
4
in
Computers & Internet
➔ Programming & Design
#include
#include
#include
using namespace std;
class Candidiate
{
public:
void GetInfo();
void Output();
//Defualt Constructor
Candidiate();
//Deconstructor
~Candidiate();
private:
int ListSize;
string *Name;
int *NumberOfVotes;
int TotalVotes;
double Percentage;
};
int main()
{
Candidiate Election;
Election.GetInfo();
Election.Output();
system("pause");
return 0;
}
Candidiate::Candidiate()
{
ListSize = 0;
TotalVotes = 0;
}
void Candidiate::GetInfo()
{
cout << "Please enter the number of candidiates" << endl;
cin >> ListSize;
Name = new string[ListSize];
NumberOfVotes = new int[ListSize];
cout << ListSize << endl;
for (int i = 0;i < ListSize;i++)
{
cout << "Please enter candidiate last name " << i + 1 << endl;
cin >> Name[i];
cout << "Please enter number of votes for " << Name[i] << endl;
cin >> NumberOfVotes[i];
TotalVotes = TotalVotes + NumberOfVotes[i];
2006-10-18
14:24:48 ·
update #1
//cout<
}
}
void Candidiate::Output()
{
//Calculate percentage of votes for each candidate
//double NumberOfVotesD = static_cast(*NumberOfVotes);
double TotalVotesD = static_cast(TotalVotes);
int Winner = 0;
int ArrayValue;
//Output Data
cout << "Candidate " << setw(5) << "Votes Received " << setw(5) << "% of Total Votes " << endl;
for (int i = 0; i < ListSize;i++)
{
Percentage = NumberOfVotes[i]/TotalVotesD * 100;
cout << Name[i] << setw(10) << NumberOfVotes[i] << setw(10) <
}
//Error Happens Here
for (int i = 1;i < ListSize + 1;i++)
{
ArrayValue = NumberOfVotes[i-1];
if (ArrayValue < NumberOfVotes[i])
{
Winner = i;
cout << "Value of winner has been changed" << endl;
}
cout << "Winner value is... " << Winner << endl << "Array Value is..." << ArrayValue << endl;
}
cout << "winner value is " << Winner << endl;
cout << "The winner of the election is " << Name
2006-10-18
14:25:07 ·
update #2
[Winner] << endl;
}
//Here ends here
Candidiate::~Candidiate()
{
delete Name;
delete NumberOfVotes;
}
2006-10-18
14:25:19 ·
update #3
Sorry I'm really fustrated with this, I forgot to say that this was written in c++.
2006-10-18
14:31:26 ·
update #4
Dude will be assigned best answer. I wanted to say thank you for helping me with my problem. I see the problem now, and have fixed it thanks to you.
2006-10-18
14:54:36 ·
update #5
THAAAAAAAAAAAAAAAAAAAAAAANNNNNNNNNNNNNNNNNNNNKKKKKKKKKKKKKKKKKKKK YOOOOOOOOOOOOOOOOOOOOOOOUUUUUUUU!!!!!!!!!!
2006-10-18
14:55:34 ·
update #6
Dude...strangly enough the program description nor the instructor said anything about error correction, or in case of a tie.
2006-10-18
14:57:41 ·
update #7
I feel so stupid, such an easy fix.
2006-10-18
15:01:47 ·
update #8