Recent

Author Topic: What is wrong with the program  (Read 2236 times)

ShockDie

  • Newbie
  • Posts: 3
What is wrong with the program
« on: January 08, 2018, 03:00:32 am »
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

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: What is wrong with the program
« Reply #1 on: January 08, 2018, 03:15:05 am »
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  [Select][+][-]
  1. var i: Integer;
  2. ...
  3. for i:=1 to 4 do begin  //begin of loop with more commands
  4. ...
  5. candidate[i];  //proper index
  6. ...
  7. end;  //end of loop
  8. ...
  9. if candidate[i]>hdboyvotes then
  10.   hdboy:=candidate[i];  //no end needed here
Pascal has no endif.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Re: What is wrong with the program
« Reply #2 on: January 08, 2018, 03:17:22 am »
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  [Select][+][-]
  1. Program Headboy(input, output);
  2. {This program will calculate the headboy and deputy headboy out of four candicates}
  3. uses crt;
  4. var
  5.         candicate: array[1..4] of string;
  6.         v, votes, candidatevotes, hdboyvotes, dhdboyvotes: integer;
  7.         hdboy, dhdboy: string;
  8. begin
  9.         clrscr;
  10.         candidatevotes := 0;
  11.         hdboyvotes     := 0;
  12.         dhdboyvotes    := 0;
  13.  
  14.         for v := 1 to 4 do
  15.           writeln('What is the amount of votes had the candidate received?');
  16.  
  17.         readln(votes);
  18.         candicate[v] := votes;
  19.  
  20.         for i := 1 to 4 do
  21.           if candicate > hdboyvotes then
  22.             hdboy := candiate
  23.           else
  24.           if candicate > dhdboyvotes then
  25.             dhdboy := candicate
  26.           endif;
  27.           endif;
  28.  
  29.         writeln('The New Headboy is', hdboy);
  30.         writeln('The New Deputy Headboy is', dhdboy);
  31. 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

 

TinyPortal © 2005-2018