Search code examples
pascallazarus

Lazarus Pascal Code not working properly cmd screen closes immediately


this is my pascal code how good it looks but not working properly. It closes directly when it should print the latest output to the screen.

Uses crt;

label durak1;

var      
  yas: Integer;

begin
  durak1:
  CLRSCR;
  write('YASINIZI BELIRTINIZ : '); READLN(yas);

  if (yas <0) or (yas >150 ) then begin 
    writeln('LUTFEN GECERLI BIR YAS ARALIGI GIRINIZ');
    delay(2000); 
    goto durak1;
  end;

  case yas of
    0,1:      writeln('Bebek Yas Grubu'); 
    2..5:     writeln('Cocuk Yas Grubu');
    6..18:    writeln('Genc Yas Grubu'); 
    19..34:   writeln('Orta Yas Grubu');
    35..59:   writeln('Hafif Yasli Grubu');
    60..100:  writeln('Bi Ayagi Cukurda Yas Grubu');
    101..150: writeln('Olmus Yas Grubu');
    else      
      writeln('Yanlis Tuslama');
      delay(10000); 
      goto durak1;
  end;
end.

this is code easy because I try to repeat what I saw at school


Solution

  • You are lacking additional ReadLN call when the user enters the valid age. Right now there is no ReadLN call after your case statement and therefore your application closes as soon as user enters valid age.

    Uses crt;
    
    label durak1;
    
    var      
      yas: Integer;
    
    begin
      durak1:
      CLRSCR;
      write('YASINIZI BELIRTINIZ : '); READLN(yas);
    
      if (yas <0) or (yas >150 ) then begin 
        writeln('LUTFEN GECERLI BIR YAS ARALIGI GIRINIZ');
        delay(2000); 
        goto durak1;
      end;
    
      case yas of
        0,1:      writeln('Bebek Yas Grubu'); 
        2..5:     writeln('Cocuk Yas Grubu');
        6..18:    writeln('Genc Yas Grubu'); 
        19..34:   writeln('Orta Yas Grubu');
        35..59:   writeln('Hafif Yasli Grubu');
        60..100:  writeln('Bi Ayagi Cukurda Yas Grubu');
        101..150: writeln('Olmus Yas Grubu');
        else      
          writeln('Yanlis Tuslama');
          delay(10000); 
          goto durak1;
      end;
      //Add another READLN call here to prevent application from closing instantly when valid age is entered
      READLN(yas);
    end.
    

    I recommend you learn how to use breakpoints and step through your code in order to observe your code flow. Knowing how to observe your code execution step by step is very important. Because this is the only way to make sure your code is executing the way you want it to.