bar graph lazarus / 39-Graphics ---------------------------------------------------------------------- SIRUS-VIRUS · 2023-05-14 17:34 Hello, guys! I need your help! Given a text file grf.txt, which contains such information: 12.03.2023  6; 13.03.2023  3; 14.03.2023 -4; ..... etc. need to draw a bar graph with numbers after the number 2023 I write next code on Pascal: USES WINCRT,GRAPH; VAR D,M,I:SMALLINT; T:TEXT; S:STRING; SZX,Y1,Y2:INTEGER; A,B:LONGINT; CODE:WORD; PROCEDURE TT(X,Y:WORD; S:LONGINT);VAR ST:STRING;BEGIN SETFILLSTYLE(SOLIDFILL,BLACK); BAR(X,Y,X+158,Y+13); SETTEXTSTYLE(1,2,1); STR(S,ST); SETCOLOR(WHITE); OUTTEXTXY(X,Y,ST); SETFILLSTYLE(SOLIDFILL,LIGHTGREEN); END; FUNCTION DN(S:STRING):LONGINT; VAR I:SMALLINT;BEGIN FOR I:=13 TO 23 DO IF S=';' THEN VAL(COPY(S,13,I-13),DN,CODE);IF S[12]='-' THEN DN:=DN*(-1);END; BEGIN M:=DETECT;INITGRAPH(D,M,'');ASSIGN(T,'GRF.TXT');RESET(T); WHILE NOT EOF(T) DO BEGIN  READLN(T,S); A:=DN(S); IF ABS(A)>B THEN B:=ABS(A) END; RESET(T); LINE(0,ROUND(GETMAXY/2)+40,GETMAXX,ROUND(GETMAXY/2)+40); SZX:=TRUNC(GETMAXX/365); SETFILLSTYLE(SOLIDFILL,LIGHTGREEN); FOR I:=1 TO 365 DO IF NOT EOF(T) THEN BEGIN  READLN(T,S); A:=DN(S); IF CODE<>0 THEN BEGIN TT(100,600+I*10,CODE);BREAK END; TT(100,600+I*10,A); Y1:=ROUND(GETMAXY/2)+40;  IF A>0 THEN Y2:=ROUND(GETMAXY/2)-ROUND(ROUND(GETMAXY/2)*ABS(A/B))+40 ELSE Y2:=ROUND(GETMAXY/2)-40+ROUND(ROUND(GETMAXY/2)*ABS(A/B));  BAR(5+SZX*(I-1)+2,Y1,5+SZX*I,Y2) END ELSE BREAK;  READKEY; END. but I can't solve the problem... in case of attached file GRF.TXT. In result of Run my program: CODE=3! basically not the work of the module GRAPH is of interest, but the work of the procedure VAL Please Help Me! ---------------------------------------------------------------------- wp · 2023-05-14 18:15 What an ugly code! You should not stuff multiple statements into a single line - this is impossible to read. And enclose in the forum code by [ code ]  [ /code] tags (without the spaces) so that it is easier to understand what is going on. And it avoids the forum software misinterpreting the index S[ I] as instruction to switch to italic font. And PLEASE, use "speaking" variable names - I have no idea what function DN() and procedure TT() are supposed to do. OK - I reformatted to code so that I can read it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; uses  WinCrt, Graph; var  D,M,I: SmallInt;  T: Text;  S: String;  SZX, Y1,Y2: Integer;  A,B: LongInt;  code: Word; procedure TT(X,Y: word; S: LongInt);var  ST: String;begin  SetFillStyle(SolidFill, Black);  Bar(X, Y, X+158, Y+13);  SetTextStyle(1, 2, 1);  Str(S, ST);  SetColor(White);  OutTextXY(X, Y, ST);  SetFillStyle(SolidFill, LightGreen);end; function DN(S: String): LongInt;var  I: SmallInt;begin  for i := 13 to 23 do    if S[i] = ';' then      val(copy(S, 13, i-13), DN, code);  if S[12] = '-' then    DN := DN*(-1);end; begin  B := 0;   M := Detect;  InitGraph(D, M, '');  Assign(T, 'GRF.TXT');  Reset(T);  while not EoF(T) do  begin    ReadLn(T, S);    A := DN(S);    if abs(A) > B then      B := abs(A)  end;  Reset(T);   Line(0, Round(GetMaxY/2)+40, GetMaxX, Round(GetMaxY/2)+40);  SZX := Trunc(GetMaxX/365);  SetFillStyle(SolidFill, LightGreen);  for i := 1 to 365 do    if not EoF(T) then    begin      ReadLn(T, S);      A := DN(S);      if Code <> 0 then      begin        TT(100, 600+I*10, code);        break;    end;      TT(100,600+I*10,A);      Y1 := round(GetMaxY/2)+40;      IF A>0 THEN        Y2:=Round(GetMaxY/2)- Round(Round(GetMaxY/2)*abs(A/B))+40      ELSE        Y2:=Round(GetMaxY/2)-40 + Round(Round(GetMaxY/2)*abs(A/B));      Bar(5+SZX*(I-1)+2,Y1,5+SZX*I,Y2)    end    else      Break;  ReadKey;end. I found only two issues: Because you did not use [ code] tags the index [ I] was missing which resulted in a logical error in function DN(). But adding it, this function worked. The other issue is that the variable B is not initialized - I set it to 0 at the begin of the program. I guess it is meant to be the maximum of the extracted file values, and therefore this initial value should be fine. ---------------------------------------------------------------------- SIRUS-VIRUS · 2023-05-14 20:25 Thank you very much for the answer! I took into account the moment with tags. I don't know why, but the program has an index "I" and still doesn't work! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i USES WINCRT,GRAPH; VAR  D,M,I:SMALLINT;  T:TEXT;  S:STRING;  SZX,Y1,Y2:INTEGER;  A,B:LONGINT;  CODE:WORD;  PROCEDURE TT(X,Y:WORD; S:LONGINT);  {Write Value to Screen}  VAR   ST:STRING;  BEGIN   SETFILLSTYLE(SOLIDFILL,BLACK);   BAR(X,Y,X+158,Y+10);   SETTEXTSTYLE(1,2,1);   STR(S,ST);   SETCOLOR(WHITE);   OUTTEXTXY(X,Y,ST);   SETFILLSTYLE(SOLIDFILL,LIGHTGREEN);  END;  FUNCTION DN(S:STRING):LONGINT;  {Extract Value from File}  VAR   I:SMALLINT;  BEGIN   FOR I:=13 TO 23 DO    IF S[I]=';' THEN     VAL(COPY(S,13,I-13),DN,CODE);    IF S[12]='-' THEN DN:=DN*(-1);  END;  BEGIN   B:=0;   M:=DETECT;   INITGRAPH(D,M,'');   ASSIGN(T,'GRF.TXT');   RESET(T);   WHILE NOT EOF(T) DO    BEGIN     READLN(T,S);     A:=DN(S);     IF ABS(A)>B THEN B:=ABS(A)    END;   RESET(T);    LINE(0,ROUND(GETMAXY/2)+40,GETMAXX,ROUND(GETMAXY/2)+40);   SZX:=TRUNC(GETMAXX/365);   SETFILLSTYLE(SOLIDFILL,LIGHTGREEN);    FOR I:=1 TO 365 DO    IF EOF(T)     THEN      BREAK     ELSE      BEGIN     READLN(T,S);       A:=DN(S);       IF CODE<>0 THEN        BEGIN       TT(100,600+I*10,CODE);         BREAK        END;       TT(100,600+I*10,A);       Y1:=ROUND(GETMAXY/2)+40;       IF A>0      THEN         Y2:=ROUND(GETMAXY/2)-ROUND(ROUND(GETMAXY/2)*ABS(A/B))+40        ELSE         Y2:=ROUND(GETMAXY/2)+ROUND(ROUND(GETMAXY/2)*ABS(A/B))-40;       IF A=0 THEN Y2:=Y1;       BAR(5+SZX*(I-1)+2,Y1,5+SZX*I,Y2)      END;   READKEY; END. ---------------------------------------------------------------------- wp · 2023-05-14 20:30 What is not working? ---------------------------------------------------------------------- SIRUS-VIRUS · 2023-05-14 20:43 Procedure VAL doesn't working as I need... CODE=3 in my case. The 4th line has the value 12, so I>15! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  VAL(COPY(S,13,I-13),DN,CODE);3th parameter I-13>2! ---------------------------------------------------------------------- wp · 2023-05-14 21:09 Your code uses val() only in the function DN(). What is this function supposed to do? I guess it intends to extract the number after the date string in a very unusual way. The loop index which iterates over the string's characters runs up to 23 but the length of the strings in the files is shorter. Of course, when it has run over the end of the string it picks random bytes, and val will not detect valid numbers any more. If you really want to extract the number, this is the way I would do it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DN(S: String): LongInt;begin  // Delete the date part, keeping the sign or the space (val accepts that)  Delete(S, 1, 11);  // Delete the trailing semicolon  if S[Length(S)] =  ';' then Delete(S, Length(S), 1);  // Convert the rest to integer  Val(S, DN, code);  // Halt in case of a conversion error  if code <> 0 then  begin    CloseGraph;  WriteLn('Invalid number detected in line "', S, '"');    Halt;  end;end; ---------------------------------------------------------------------- SIRUS-VIRUS · 2023-05-14 22:55 Thank You! it works now!