I'm fairly new to programming so please be gentle guys because I know this may seem like a stupid question. In this simple polling program I have written, I can not seem to get my number of votes to accumulate nor the percent to calculate and display correctly. Could some one please tell me what I'm doing wrong and possibly lend me some tips for future programs.
static void Main(string[] args){
string[] candidates = new string[5] { "Hillary Clinton", "Barack Obama", "John McCain", "Ralph Nader", "None of these are my favorite" };
double totalVote = 0,
votePercent = 0;
int choice1 = 0,
choice2 = 0,
choice3 = 0,
choice4 = 0,
choice5 = 0, numOfVotes = 0, VoteSelect = 0;
char altPoll;
do
{
Console.WriteLine("Opinion Poll with Functions\n\n");
Console.WriteLine("***********OPINION POLL***********");
for (int i = 0; i < 5; i++)
{
Console.WriteLine(candidates[i]);
}
Console.Write("\nPlease choose your favorite candidate based on its corresponding number=> ");
VoteSelect = (Convert.ToInt32(Console.ReadLine()));
Console.WriteLine("\nQuestion....\n");
Console.Write("Would you like to do this again (y/n): ");
altPoll = (Convert.ToChar(Console.ReadLine()));
}
while (altPoll == 'y' || altPoll == 'Y');
if (altPoll == 'n' || altPoll == 'N')
{
if (VoteSelect == 1)
{
numOfVotes += choice1++;
}
if (VoteSelect == 2)
{
numOfVotes += choice2++;
}
if (VoteSelect == 3)
{
numOfVotes += choice3++;
}
if (VoteSelect == 4)
{
numOfVotes += choice4++;
}
if (VoteSelect == 5)
{
numOfVotes += choice5++;
}
totalVote += numOfVotes;
votePercent = VoteSelect/ totalVote;
}
Console.WriteLine("\nCANDIDATE\t\tVOTES\t\tPERCENTAGE");
Console.WriteLine("____________________________________________________");
Console.WriteLine("Hilary Clinton \t\t {0} \t\t{1:P}",choice1,votePercent);
Console.WriteLine("Barack Obama \t\t {0} \t\t{1:P}",choice2,votePercent);
Console.WriteLine("John McCain \t\t {0} \t\t{1:P}",choice3,votePercent);
Console.WriteLine("Ralph Nader \t\t {0} \t\t{1:P}",choice4,votePercent);
Console.WriteLine("Not Offered \t\t {0} \t\t{1:P}",choice5,votePercent);
}
}
}
No comments:
Post a Comment