Forum > General

What is wrong with the program

(1/1)

ShockDie:
I am trying to create a program to calculate the headboy and deputy headboy out of four candidates
https://gyazo.com/fc256821a26f8c194633a998dd059b10
https://gyazo.com/4f3ff1e92f40bc88b792e7f93e6f0664

Blaazen:
You need proper loop variable and proper indexing of array in loop, also you need begin..end block when there are more commands in loop or in if..then..else:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var i: Integer;...for i:=1 to 4 do begin  //begin of loop with more commands...candidate[i];  //proper index...end;  //end of loop...if candidate[i]>hdboyvotes then  hdboy:=candidate[i];  //no end needed herePascal has no endif.

Handoko:
Hello ShockDie,
Welcome to the forum.

You can use code tag for putting your code to the forum. Here I've made it for you:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Program Headboy(input, output);{This program will calculate the headboy and deputy headboy out of four candicates}uses crt;var        candicate: array[1..4] of string;        v, votes, candidatevotes, hdboyvotes, dhdboyvotes: integer;        hdboy, dhdboy: string;begin        clrscr;        candidatevotes := 0;        hdboyvotes     := 0;        dhdboyvotes    := 0;         for v := 1 to 4 do          writeln('What is the amount of votes had the candidate received?');         readln(votes);        candicate[v] := votes;         for i := 1 to 4 do          if candicate > hdboyvotes then            hdboy := candiate          else          if candicate > dhdboyvotes then            dhdboy := candicate          endif;          endif;         writeln('The New Headboy is', hdboy);        writeln('The New Deputy Headboy is', dhdboy);end.
By formatting the code properly, it is easy to see that:
- You use for loop incorrectly
- Pascal does not use endif

Read more:
http://wiki.lazarus.freepascal.org/FOR..DO
https://www.freepascal.org/docs-html/3.0.0/ref/refsu58.html

Well, Blaazen is faster.  :D

Navigation

[0] Message Index

Go to full version