English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

4 answers

I'm not sure what you were trying to do in that loop, but here's how I'd handle it: (please excuse any syntax mistakes, I haven't written C++ for a while)

winner = 0;
for (int x=0; x if (NumberOfVotes[x] > NumberOfVotes[winner]) winner = x;
}

cout << "The winner is: " << names[winner];

##########

That oughta do it. But what if there's a tie? You could make winner an array. That's your homework assignment.

2006-10-18 14:45:18 · answer #1 · answered by dude 1 · 0 0

Your loop is always comparing the current value ([i]) of the array to the previous one (ArrayValue=...[i-1])
I would guess, that you'd want to compare to the maximum value seen so far, not just the previous one?
try something like this instead

for (int i = 0, Winner=0; i < ListSize; i++)
{
if (NumberOfVotes[i]) > NumberOfVotes[Winner])
{
Winner = i;
max = NumberOfVotes[i];
}
}

2006-10-18 22:02:41 · answer #2 · answered by n0body 4 · 0 0

Dude has the right idea. Your code goes wrong here:
ArrayValue = NumberOfVotes[i-1];
if (ArrayValue < NumberOfVotes[i])

You're comparing against the previous candidate in the list, when you should be comparing against the highest vote count so far.

2006-10-18 21:52:24 · answer #3 · answered by injanier 7 · 0 0

Maybe its your clicker!Hollywood

2006-10-18 21:32:20 · answer #4 · answered by hollywood 5 · 0 0

fedest.com, questions and answers