Recent

Author Topic: Help with error Constant Expression expected  (Read 3087 times)

vodka8

  • Newbie
  • Posts: 3
Help with error Constant Expression expected
« on: March 24, 2015, 11:45:35 am »
Hey,

in my var I have this var KLEUR:array[1..4] of Integer;

Then I have a procedure that puts a number in the column,
for I:=1 to 4 do
  begin
    KLEUR := RANDOM(4)+1;
  end;                     

In another procedure I have a case where I need to check that what the user entered is what the random generated so:
case (A) of
      KLEUR[1]:
        begin
          writeln('R');
        end;
      KLEUR[2]:
        begin
          writeln('W');
        end;
      KLEUR[3]:
        begin
          writeln('W');
        end;
      KLEUR[4]:
        begin
          writeln('W');
        end;
      else
        writeln('-');
      end;               

I allready searched google and found that I'll have to work with an IF and ELSEIF, but this will be very deep instead of the case.

My question is if there is a way that I could get this working, maybe changing sth in my vars?

Thanks in advance for the help!

Eugene Loza

  • Hero Member
  • *****
  • Posts: 579
Re: Help with error Constant Expression expected
« Reply #1 on: March 24, 2015, 12:04:01 pm »
First of all, you cannot assign a whole array at once, just a single element:
Code: [Select]
KLEUR[i] := RANDOM(4)+1;Secondly, you seem to solve the problem in a wrong way. I.e. you have, let suggest, KLEUR = array(1,1,1,1) - it's random and this vairant is possible. And you compare it to input 'A'. Seems strange to me. What do you want the user do? Guess?
It seems that if the number is not KLEUR[1] the answer is 'W' i.e. wrong... why so?
But if that logic is ok, then just do:
answer = '-';
if A=KLEUR[1] then ... 'R' else
for i:=2 to 5 do if A=KLEUR then .. 'W';
Because Case is not designed to work with non-constants which may have duplicate values like in example above. e.g. which of five '1's the case should choose? So use 'if' construction
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

vodka8

  • Newbie
  • Posts: 3
Re: Help with error Constant Expression expected
« Reply #2 on: March 24, 2015, 12:16:18 pm »
Hey, thanks for your fast answer.
Indeed I forgot the KLEUR on the forumpost.
Actually we are writing a simple mastermind in class, but as I'm the only one working with arrays & case it gets the problem.

This is the whole code of the procedure of guessing after it randomizes:

procedure RADEN;

{ declare your variables here }

begin
  POGINGEN:=0;
  writeln('GEEF 4 CIJFERS IN VAN 1 TEM 4');
  readln(A,B,C,D);
  writeln('UW POGING IS:');
  writeln(A B C D);
  repeat
    case (A) of
      KLEUR[1]:
        begin
          writeln('R');
        end;
      KLEUR[2]:
        begin
          writeln('W');
        end;
      KLEUR[3]:
        begin
          writeln('W');
        end;
      KLEUR[4]:
        begin
          writeln('W');
        end;
      else
        writeln('-');
      end;
    case (B) of
      KLEUR[1]:
        begin
          writeln('W');
        end;
      KLEUR[2]:
        begin
          writeln('R');
        end;
      KLEUR[3]:
        begin
          writeln('W');
        end;
      KLEUR[4]:
        begin
          writeln('W');
        end;
      else
        writeln('-');
      end;
    case (C) of
      KLEUR[1]:
        begin
          writeln('W');
        end;
      KLEUR[2]:
        begin
          writeln('W');
        end;
      KLEUR[3]:
        begin
          writeln('R');
        end;
      KLEUR[4]:
        begin
          writeln('W');
        end;
      else
        writeln('-');
      end;
    case (D) of
      KLEUR[1]:
        begin
          writeln('W');
        end;
      KLEUR[2]:
        begin
          writeln('W');
        end;
      KLEUR[3]:
        begin
          writeln('W');
        end;
      KLEUR[4]:
        begin
          writeln('R');
        end;
      else
        writeln('-');
      end;
    writeln('DOE EEN NIEUWE POGING:');
    readln(A,B,C,D);
    POGINGEN:=POGINGEN+1;
  until (A=KLEUR[1])AND(B=KLEUR[2])AND(C=KLEUR[3])AND(D=KLEUR[4]);
  writeln('************ PROFICIAT ******************');
  writeln('U HEBT DE JUISTE CODE GERADEN');
  writeln('AANTAL POGINGEN: ',POGINGEN);
end;                                         


vodka8

  • Newbie
  • Posts: 3
Re: Help with error Constant Expression expected
« Reply #3 on: March 24, 2015, 12:17:02 pm »
But you gave the answer it's not possible with cases, thanks man!

 

TinyPortal © 2005-2018