1

Just started to learn the Pascal programming language, I wrote an assignment that was given at the university, here is my assignment:

Build a program that will perform the following actions:

  • retrieve data of N elements (brand, name, atomic weight, density)
  • find the average density value
  • finding the three elements with the lowest atomic mass
  • writing the inputs and outputs to the text file "elements.txt"

wrote code for it, in theory everything should work, but it gives out some strange errors, if I have standard errors, I apologize for them.

program MINERALY_;
uses    Crt;
const   NMAX=200;
type    MINERAL=record
      NAZOV:string;
      HUST:real;
      ATOM:real;
      ZNACKA:real;
      end;
    MINERALY=array[1..NMAX] of MINERAL;

    var     N,i,C,D:integer;
    V:real;
    MI:MINERALY;
    S:string;
    SUB:text;
    PTVRD,CTVRD:real;
    MINATOM:real;


    begin
    ClrScr;
    Writeln('Program pre nacitanie mineralov a vypocet hodnot ich jednotlivych 
    vlastnosti.');
    Writeln('===============================================================================');

    {Vynulovanie hodnot}
    CTVRD:=0;
    PTVRD:=0;
    MINATOM:=0;


    {Nacitanie poctu mineralov}
    Writeln;
    Write('Zadajte pocet nacitavanych mineralov (Maximalne 200): ');
    repeat
            Readln(S);
            Val(S,D,C);
            if (C<>0) or (D<=0) or (D>200) then
                    begin
                    Writeln;
                    Writeln('Zadali ste nespravny pocet, alebo ste zadali nespravne znaky !');
                    Writeln('Hodnota moze byt  od 1-200');
                    Write('Zadajte pocet mineralov este raz: ');
                    end;
            N:=D;
    until(N>0) and (N<=200) and (C=0);

    {nacitavanie udajov, zistovanie max. a min. hodnot atd.}
    for i:=1 to N do
            begin
                    Writeln;
                    Write('Zadajte Znacku: ');
                    Readln(MI[i].ZNACKA);
                    
                    Writeln;
                    Write('Zadajte nazov: ');
                    Readln(MI[i].NAZOV);
                    
                    Write('Zadajte hustotu (musi byt vacsia ako 0): ');
                    repeat
                            Readln(S);
                            Val(S,V,C);
                            if (C<>0) or (V<0) then
                                    begin
                                    Writeln;
                                    Writeln('Zadali ste nespravnu hodnotu, alebo ste zadali nespravne znaky !');
                                    Write('Zadajte hustotu este raz (musi byt vacsia ako 0): ');
                                    end;
                    until (V>0) and (C=0);
                    MI[i].HUST:=V;
                    CTVRD:=CTVRD+MI[i].HUST;

                    Write('Zadajte atómovou hmotnosťou (musi byt vacsia ako 0): ');
                    repeat
                            Readln(S);
                            Val(S,V,C);
                            if (C<>0) or (V<0) then
                                    begin
                                    Writeln;
                                    Writeln('Zadali ste nespravnu hodnotu, alebo ste zadali nespravne znaky !');
                                    Write('Zadajte atómovou hmotnosťou este raz (musi byt vascia ako 0): ');
                                    end;
                            until (V>0) and (C=0);
                    MI[i].ATOM:=V;

                    if (i=1) then MINATOM:=MI[i].ATOM;
                    if (MI[i].ATOM<MINATOM) then MINATOM:=MI[i].ATOM;




    PTVRD:=CTVRD/N;

    Writeln;
    Writeln('Vypocitane hodnoty: ');
    Writeln('===================================');
    Writeln;
    Writeln('Priemerna tvrdost mineralov: ',PTVRD:2:2);
    Writeln('Najnizsia hustota: ',MINATOM:2:2);



    Assign(SUB,'mineral.txt');
    Rewrite(SUB);
    Writeln(SUB,'Nacitane mineraly: ');
    Writeln(SUB,'===================================');
    Writeln(SUB,'');
    
    for i:=1 to N do
            begin
            Writeln(SUB,i,'. mineral: ');
            Writeln(SUB,'Nazov: ',MI[i].NAZOV);
              Writeln(SUB,'Znacka: ',MI[i].ZNACKA);
            Writeln(SUB,'Tvrdost: ',MI[i].HUST:2:2);
            Writeln(SUB,'Hustota: ',MI[i].ATOM:2:2);
            Writeln(SUB,'');
    end;
    Writeln(SUB,'===================================');
    Writeln(SUB,'Vypocitane hodnoty: ');
    Writeln(SUB,'===================================');
    Writeln(SUB,'');
    Writeln(SUB,'Priemerna tvrdost mineralov: ',PTVRD:2:2);
    Writeln(SUB,'Najnizsia hustota: ',MINATOM:2:2);


    Close(SUB);
    Writeln;
    Writeln;
    Writeln('Pre ukoncenie programu stlacte ENTER !');
    Readln;

  end.

Here are the errors this code gives me:

Compiling main.pas
main.pas(109,14) Error: Illegal assignment to for-loop variable "i"
main.pas(132,4) Fatal: Syntax error, ";" expected but "." found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Karaoki
  • 11
  • 2
  • 1
    https://www.qc-s.com/download/ObjectPascalStyleGuide.html Your code is kind of hard to read. You are using CAPITAL LETTERS for all your variable names, an indentation is inconsistent. Also, I am unfamiliar with Slovak, but it does look like you are using unnecessary abbreviations with a number of your variable names. Prefer clarity over brevity. – Dúthomhas Jan 18 '23 at 04:07

1 Answers1

1

Error: Illegal assignment to for-loop variable "i"

is when you are trying to change the value of i while the loop is still working.

since you didn't end; the loop you canno't change the value of i you can use another variable, j for example, unless your first for-loop is over and you forgot the end; key

Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38