: : : I'm a newbie and need some help desperately!!!
: : : My question goes like this:
: : :
: : : A company is voting for a chairmen and is recorded by entering the numbers 1 to 5 depending on which of the five candidates secured a vote. Enter 0 toindicate that all votes have been input. It must print how many votes for each candidate. Once all the votes have
: : : been entered, print the total for each candidate and the total of all the valid votes.
: : :
: : : I need some help with the control statements, I don't really know how to code them properly.
: : : Thank you
: : :
: : The best way to do this is to create an NumberOfVotes array with 5 elements, one for each candidate.
: : Then if someone inputs a vote, increase that number by 1, like so:
: :
: : readln(CastVote);
: : if CastVote > 0 then
: : NumberOfVotes[CastVote] := NumberOfVotes[CastVote] + 1;
: :
: : Around this simple setup you can then create a repeat-until loop to keep on voting until some-one casts a vote on candidate 0.
: : Writing the number of votes for each candidate is simply a for-do loop with a writeln() statement and the NumberOfVotes array.
: :
: Can anyone show me how to fix this without using arrays. I need a very simple method.
: Thank you
:
You can use 5 variables with an if-then-else statement like this:
if CastVote = 1 then
Candidate1 := Candidate1 + 1
else if CastVote = 2 then
Candidate2 := Candidate2 + 1
else if CastVote = 3 then
Candidate3 := Candidate3 + 1
{etc.}
The rest stays pretty much the same way.