0
votes

I am unable to locate the source of this error that i keep getting in my coding for school. Every time i enter one value in my array and the for loop runs it either encounters a runtime error or the rest of the program runs based on information from the first value and doesn't accept any other values. Can someone please explain to me how to fix this?

program Montserrat_Elections;

Var cndnme : array [1..4] of String;
    votes : array [1..4] of Integer;
    highest, cnt : Integer;
    winner : string;

begin

 highest:= 0;
 winner:= 'Dan';

 For cnt:= 1 to 4 do

     begin
          Writeln('Please enter the first name of the candidate and the number of votes');
          Read (cndnme[cnt], votes[cnt]);

          If votes[cnt] > highest then
             highest := votes[cnt];
             winner := cndnme[cnt];
     end;
 Writeln('The winner of this constituency is', winner, 'with', highest, 'votes')
end.
1
Read about single statement and compound statement. You need a begin/end around your if body. - David Heffernan
You can't read a string and an integer in one READ command, since what would be the separator that separates a string from an integer? A space is also allowed for read(something_string) - Marco van de Voort
@David: The issue is the Read statement; it has nothing to do with the if. - Ken White
@ken It is still a logic error isn't it. - David Heffernan
@David: It has absolutely nothing to do with the problem described in the question. - Ken White

1 Answers

1
votes

Change Read to Readln :

Readln (cndnme[cnt], votes[cnt]);

Then you need add begin...end; to this line:

If votes[cnt] > highest then
      begin
         highest := votes[cnt];
         winner := cndnme[cnt];
      end;

I update & test your codes :

program Montserrat_Elections;

Var cndnme : array [1..4] of String;
    votes : array [1..4] of Integer;
    highest, cnt : Integer;
    winner : string;

begin
 highest:= 0;
 winner:= 'Dan';

 For cnt:= 1 to 4 do

 begin
      Writeln('Please enter the first name of the candidate and the number of votes');
      readln(cndnme[cnt], votes[cnt]);

      If votes[cnt] > highest then
      begin
         highest := votes[cnt];
         winner := cndnme[cnt];
      end;

 end;
 Writeln('The winner of this constituency is ', winner, ' with ', highest, ' votes');
 readln;
end.

Result:

Please enter the first name of the candidate and the number of votes                                                                                                            
Me                                                                                                                                                                              
23                                                                                                                                                                              
Please enter the first name of the candidate and the number of votes                                                                                                            
You                                                                                                                                                                             
42                                                                                                                                                                              
Please enter the first name of the candidate and the number of votes                                                                                                            
Ainun                                                                                                                                                                           
18                                                                                                                                                                              
Please enter the first name of the candidate and the number of votes                                                                                                            
Jhon                                                                                                                                                                            
38                                                                                                                                                                              
The winner of this constituency is You with 42 votes