0

Please help me my program just keeps on displaying the last name and number here is the program. I really dont see what is wrong here but I do hope that you guys may see it please really need some help its killing my brain

Var
Counter:Integer;
MaxValue:Integer;
NumofVotes:Array[1..4]of Integer;
ChristianName:Array[1..4]of String;
Surname:Array[1..4]of String;
WinnerFName:String;
WinnerSName:String;
WinnerParty:String;
CandidateParty: Array[1..4] of String;

begin
  FOR Counter:= 1 to 4 Do
  If Counter= Counter Then
  begin
Writeln ('Please enter Christian name of Candidate ', Counter, ':');
Readln (ChristianName[Counter]);
Writeln ;
Writeln ('Please enter Surname of Candidate ', Counter, ':');
Readln (Surname[Counter]);
Writeln ;
Writeln ('Please enter number of votes received by Candidate ', Counter, ':');
Readln (NumOfVotes[Counter]);
Writeln ;
Writeln ('Please enter party of Candidate ', Counter, ':');
Readln (CandidateParty[Counter]);

  end;

  IF Counter = 1 THEN
begin
MaxValue:= NumofVotes[Counter];
WinnerFName:= ChristianName[Counter];
WinnerSName:= Surname[Counter];
WinnerParty:= CandidateParty[Counter];
end

ELSE
IF (NumofVotes[Counter]>MaxValue) THEN
begin
WinnerFName:= ChristianName[Counter];
WinnerSName:= Surname[Counter];
MaxValue:= NumofVotes[Counter];
WinnerParty:= CandidateParty[Counter]
end;
Writeln ;
Writeln ('The winner of the elections for this constituency is:');
Writeln ('FirstName: ', WinnerFName, ' Surname: ', WinnerSName);
Writeln ('From the ', WinnerParty);
Writeln (WinnerFName, ' has won with ', MaxValue, ' votes');
Writeln ;
Writeln  ;
Writeln ('Press <Enter> to end');
Readln  ;

end.
  • `If Counter= Counter Then` .. what kind of a test is that? A Counter is always a Counter.   `IF Counter = 1 THEN` .. you can't use 'for' loop index variable after the loop in pascal. Even if you could, how could it be '1', the last time it was '4'. – Sertac Akyuz May 16 '14 at 02:52
  • I did that because when I didn't do that the program came up weird it displayed all four questions at the same time instead of displaying them sequentially – user3643170 May 16 '14 at 02:59
  • Sorry I just took that out and it still works thanks much but what about it no testing for the maximum value?? – user3643170 May 16 '14 at 03:04
  • 1
    If you properly format your code, you'll be able to see (by the level of indent) that the flow doesn't go quite like you think it does. **Learn to format your code** so you can read it and follow the execution path. (The **emphasis** is because this has been said here hundreds of times, and no one ever seems to notice it. It gets rather frustrating having to repeat yourself over and over again.) With that being said, **learn to format your code properly**. :-) For a clear example of why this is important, see [this answer](http://stackoverflow.com/a/23681072), which I didn't write. – Ken White May 16 '14 at 03:07
  • Thanks much sir i'm just in high school trying to do some programming cuz I really like programming but i seemed to have got it to work here is what I got – user3643170 May 16 '14 at 03:29
  • @user - Counter is not 1. So take out the 'if counter = 1' block. What do you have? 'IF (NumofVotes[Counter]>MaxValue) THEN'. But you never assigned anything to 'MaxValue'. And there's no loop to compare anything after you read the values. – Sertac Akyuz May 16 '14 at 03:30
  • Sorry looks like i cant put it in inless its 8 hours after but ill show after – user3643170 May 16 '14 at 03:49
  • Also, not to nitpick but a Christian name is one given at baptism, traditionally. Not everyone is baptized, nor is everyone even of the religion where that might be a consideration. – JohnP May 19 '14 at 22:41

1 Answers1

0

The code seems to lack one pair of begin ... end keyword. As a result, the for instruction ends at the first end keyword, that is just after reading all candidates data. Then the Counter is 4 and the comparision in IF Counter = 1 fails – execution proceeds to ELSE. There the uninitialized MaxValue is probably zero, so the fourth NumofVotes is greater than that, which eventually causes the fourth candidate to be printed.

To make the code do what you expected, add begin just after the FOR ... DO and close it with end just before Writeln ; Writeln ('The winner ... is:');

CiaPan
  • 9,381
  • 2
  • 21
  • 35