I am supposed to create a Pascal program that obtains 4 inputs from user and averages them. The program should enforce all inputs to be between 0 and 100, inclusive. This is for class, but more importantly I have more programs and I need to understand why I suck at this. Not looking for an easy answer. I appreciate any help.

ERROR messages
pro4.pas(15,20) Warning: Variable NUM does not seem to be initialized
pro4.pas(25) Fatal: Unexpected end of file


PROGRAM pro4(input,output);

VAR

tot, num, i: integer; ave: real;

BEGIN

ave:= tot/4;

for i:= 1 to 4 do begin

writeln (output, 'Enter integer');

readln (input, num + tot);

while(num>=0) and (num<=100) do begin

writeln(output, 'Get another integer');

readln(input, ave);

end ;

Dani AI

Generated

Summary of the real problems and a concise fix plan.

  • The compiler warnings indicate two distinct issues: an accumulator used before it’s given a value (initialize the total to zero), and a mismatched/missing block terminator (every begin needs a matching end and the whole program must finish with end.). correctly flagged the missing terminator and laid out the correct structure; the following fills in robustness and input-parsing details not fully covered above.

  • Read semantics and accumulation: readln must read into a variable — an expression like num + tot cannot appear there. Read the user input into a variable, validate it, then update the accumulator with something like tot := tot + num. Validate each of the four inputs with a small re-prompt loop (repeat..until or while) that only exits when the entered value is within 0..100.

  • Guard against non-numeric input: classic Pascal provides val(string, value, code) (check code <> 0 for errors). In FreePascal/Delphi TryStrToInt (SysUtils) is another safe option. Example patterns:

    { classic }
    val(s, n, code);
    if code <> 0 then writeln('not a number') else total := total + n;
    { FreePascal/Delphi }
    if not TryStrToInt(s, n) then writeln('not a number') else total := total + n;
  • Type and output notes: compute the average after all four valid inputs. Use real division (make one operand real or cast) to avoid integer truncation. Format the printed average to a sensible number of decimals (e.g. two) for readability. Test boundary values (0 and 100), several invalid numbers, and non-numeric input. Compile often, follow indentation to spot missing ends, and treat warnings as clues, not annoyances.

Acknowledgement: the corrected structure shown by plus these parsing and testing tips will produce a small, robust console program that meets the assignment requirements.

Recommended Answers

All 5 Replies

readln (input, num + tot); uh...
where's your end. to end the program

See comments in code:

program pro4(input, output);
var
   tot, num, i: integer;
   ave: real;
begin
   ave := tot / 4; { tot hasn't been initialized yet. Can't do this here. }

   for i := 1 to 4 do
   begin
      writeln(output, 'Enter integer');
      readln(input, num + tot); { You can't perform math operation here. }
      {
         The following loop will be keep repeating until the user
         enters a value outside the valid range. I don't think
         that's what you want.
      }
      while (num >= 0) and (num <= 100) do
      begin
         writeln(output, 'Get another integer');
         readln(input, ave); { why are you reading into ave? }
      end;
   end; { for loop } { This was missing. }
end. { program } { This was missing. }

Revised code (compiles with FreePascal / WinXP):

program Program4;
uses
   SysUtils;
var
   Total: Integer;
   Number: Integer;
   I: Integer;
   Average: Double;
begin
   { Initialize Total to zero. }
   Total := 0;

   { Get four integer values from the user. }
   for I := 1 to 4 do
   begin
      Write('Enter an integer value between 0 and 100 (inclusive): ');
      ReadLn(Number);
      while (Number < 0) or (Number > 100) do
      begin
           WriteLn;
           WriteLn('Error: Number out of range. Please re-enter.');
           WriteLn;
           Write('Enter an integer value between 0 and 100 (inclusive): ');
           ReadLn(Number);
      end;
      Total := Total + Number;
   end;

   { Calculate the average. }
   Average := Total / 4.0;

   { Display the average. }
   WriteLn;
   WriteLn('The average is: ' + FloatToStr(Average));
   WriteLn;
end.

Thank you both very much. I managed to put everything together, and met with my professor for a little more understanding. I really appreciate each of you being helpful instead of bashing me for not being smart enough to creat a simple program.

ehh
i'm in coding a go game on pascal. this is the code as far
program IGOPascal;

uses
wincrt;

type
igo=0..3;

var
papan:array[1..15,1..15]of igo;
xt,yt:array[0..225]of integer;
tanda:array[1..4]of boolean;
a,b,x,y,i,j,pin:integer;

procedure cek_d1(xd:integer;yd:integer;pin:integer);
begin
case papan[xd,yd] of
0:exit;
1:cek_c(xd,yd,pin);
2:begin
tanda[j]:=true;
exit;
end;
end;

procedure cek_d2(xd:integer;yd:integer;pin:integer);
begin
case papan[xd,yd] of
0:exit;
2:cek_c(xd,yd,pin);
1:begin
tanda[j]:=true;
exit;
end;
end;
procedure cek_c(xc:integer;yc:integer;pin:integer);

begin
pin:=pin+1;
for j:=1 to 4 do
begin
case j of
2:begin
xt[pin]:=xc+1;
yt[pin]:=yc;
end;

1:begin
xt[pin]:=xc-1;
yt[pin]:=yc;
end;

4:begin
xt[pin]:=xc;
yt[pin]:=yc+1;
end;

3:begin
xt[pin]:=xc;
yt[pin]:=yc-1;
end;
end;
case papan[x,y] of
1:cek_d1(xt[0],xy[0]);
2:cek_d2(xt[0],xy[0]);
end;
end;
if tanda=true then
papan[xc,yc]:=3;
end;

procedure cek_b1(xb:integer;yb:integer);

begin
case papan[xb,yb] of
0:exit;
1:exit;
2:cek_c(xb,yb,0);
end;

procedure cek_b2(xb:integer;yb:integer);

begin
case papan[xb,yb] of
0:exit;
2:exit;
1:cek_c(xb,yb,0);
end;

procedure cek_igo(xa:integer;ya:integer);

begin
for i:=1 to 4 do
begin
case i of
1:begin
xt[0]:=xa+1;
yt[0]:=ya;
end;

2:begin
xt[0]:=xa-1;
yt[0]:=ya;
end;

3:begin
xt[0]:=xa;
yt[0]:=ya+1;
end;

4:begin
xt[0]:=xa;
yt[0]:=ya-1;
end;
end;
case papan[x,y] of
1:cek_b1(xt[0],xy[0]);
2:cek_b2(xt[0],xy[0]);
end;
end;
for i:=1 to 225 do
for j:=1 to 225 do
if papan[j,i]=3 then
papan[j,i]=0;

end;


begin
readln(x);
readln(y);
cek_igo(x,y);
end.


it use a multiple procedure
can someone help me?

Make a new thread as the intiial post was answered and this is not your thread. Post with code tags so your code is readable. As well as explain the problem you're having.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.