2017-01-26 4 views
0

Ich bin nicht in der Lage, die Quelle dieses Fehlers zu finden, die ich in meiner Codierung für die Schule bekomme. Jedes Mal, wenn ich einen Wert in mein Array eintrage und die for-Schleife läuft, trifft es entweder auf einen Laufzeitfehler oder der Rest des Programms läuft auf der Basis von Informationen aus dem ersten Wert und akzeptiert keine anderen Werte. Kann mir bitte jemand erklären, wie ich das beheben kann?unbekannter Laufzeitfehler in Pascal

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

Lesen Sie mehr über einzelne Anweisung und Compound-Anweisung. Du brauchst ein Anfang/Ende um deinen Körper herum. –

+1

Sie können eine Zeichenfolge und eine ganze Zahl nicht in einem READ-Befehl lesen, denn was wäre das Trennzeichen, das eine Zeichenfolge von einer Ganzzahl trennt? Ein Leerzeichen darf auch gelesen werden (something_string) –

+0

@David: Das Problem ist die 'Read' Anweisung; es hat nichts mit dem 'if' zu tun. –

Antwort

1

ändern lesen zu Readln:

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

Dann brauchen Sie beginnen hinzufügen ... end; zu dieser Zeile:

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

I aktualisieren & Test Ihre 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. 

Ergebnis:

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