Hi all, I have been creating a program that checks the length of an inputted value. The value inputted needs to be between two specific bounds, e.g. 10 < length < 120.

However, I want to ensure that an individual cannot enter text, or a value which is not between this range. At present the code I have looks like this:

program length;

uses

crt, sysutils;

 var
    number: string[9];
    length: real;
  

begin
    Clrscr;
    Writeln('Please enter your the Length: '  );
    writeln;
    readln(number);
    repeat
      try
        length := StrToFloat(number);
        if (length < 10) or (length > 120) then
        begin
          Clrscr;
          writeln('An error has occured. Please try again.');
          writeln;
          readln(number);
        end;
      except
        clrscr;
        writeln('An error has occured. Please try again.');
        writeln;
        readln(number);
      end;
    until (length >= 10) and (length <= 120);
    clrscr;
       Writeln('Your length is: ', length:0:2 );
       readln;
  end.

The code does not bring up any errors but it doesn't work the correct way either. If a user were to input text at first, it will bring up an error message. They then get to input a new value for the length. However when they input this value it doesn't work. It outputs the length as 0!

Please could someone help, feel free to rewrite the code :)

Thanks

Greg

I did not find any error in your code above ....
Below is a variant of your program, because Delphi 7 doe not work with ClrScr:

program checklength;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  R:    Real;
  S:    string[9];
begin
  R := 0;
  //Clrscr;
  Write('Please enter your the Length: ');
  Readln(S);
  repeat
    try
      R := StrToFloat(S);
      if (R<10) or (R>120) then
      begin
        //Clrscr;
        if (R<10) then
          Writeln('Your length is less than  10. Please enter again.')
        else
          Writeln('Your length is more than 120. Please enter again.');
        Writeln;
        Write('Please enter your the Length: ' );
        Readln(S);
      end;
    except
      //clrscr;
      Writeln;
      Writeln('An error has occured. Please try again.');
      Write('Please enter your the Length: ' );
      Readln(S);
    end;
  until ((R>=10) and (R<=120));
  //clrscr;
  Writeln;
  Writeln('Correct input.');
  Writeln('YOUR LENGTH IS:  ', R:0:2 );
  Writeln('End of Program.');
  Readln;
end.

I did not find any error in your code above ....
Below is a variant of your program, because Delphi 7 doe not work with ClrScr:

program checklength;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  R:    Real;
  S:    string[9];
begin
  R := 0;
  //Clrscr;
  Write('Please enter your the Length: ');
  Readln(S);
  repeat
    try
      R := StrToFloat(S);
      if (R<10) or (R>120) then
      begin
        //Clrscr;
        if (R<10) then
          Writeln('Your length is less than  10. Please enter again.')
        else
          Writeln('Your length is more than 120. Please enter again.');
        Writeln;
        Write('Please enter your the Length: ' );
        Readln(S);
      end;
    except
      //clrscr;
      Writeln;
      Writeln('An error has occured. Please try again.');
      Write('Please enter your the Length: ' );
      Readln(S);
    end;
  until ((R>=10) and (R<=120));
  //clrscr;
  Writeln;
  Writeln('Correct input.');
  Writeln('YOUR LENGTH IS:  ', R:0:2 );
  Writeln('End of Program.');
  Readln;
end.

The program itself works, but not with the desired results.

Lets say I enter 'tt' at first. This will bring up an error message and ask the user to input a new value. The user inputs a new value which is correct, say 20.45, this is checked to ensure it is correct and then the program proceeds. However, when this is written to the screen, it comes out as 0.

If the user inputs a number first time round which is valid then no problem arises, their height (with decimals) is outputted to the screen just fine.

Perhaps you could suggest a better way I write this code, just doesn't feel/look right.

Thanks

Greg :)

The program itself works, but not with the desired results.

Lets say I enter 'tt' at first. This will bring up an error message and ask the user to input a new value. The user inputs a new value which is correct, say 20.45, this is checked to ensure it is correct and then the program proceeds. However, when this is written to the screen, it comes out as 0.

If the user inputs a number first time round which is valid then no problem arises, their height (with decimals) is outputted to the screen just fine.

Perhaps you could suggest a better way I write this code, just doesn't feel/look right.

Thanks

Greg :)

Please try the next code, it avoids real exception using procedure Val():

program length;

{$APPTYPE CONSOLE}

uses
  SysUtils, System;

var
  I:    Integer;
  R:    Real;
  S:    string[9];
begin
  //Clrscr;
  Write('Please enter your the Length: ');
  Readln(S);
  repeat
    Val(S, R, I);
    if (I=0) then
    begin
      if (R<10) or (R>120) then
      begin
        //Clrscr;
        if (R<10) then
          Writeln('Your length is less than  10. Please enter again.')
        else
          Writeln('Your length is more than 120. Please enter again.');
        Writeln;
        Write('Please enter your the Length: ' );
        Readln(S);
      end;
    end
    else
    begin
      //clrscr;
      Writeln;
      Writeln('An error has occured. Please try again.');
      Write('Please enter your the Length: ' );
      Readln(S);
    end;
  until ((R>=10) and (R<=120));
  //clrscr;
  Writeln;
  Writeln('Correct input.');
  Writeln('YOUR LENGTH IS:  ', R:0:2 );
  Writeln('End of Program.');
  Readln;
end.

Brilliant Finalist- you've saved the day again!

Thanks

Greg :)

Brilliant Finalist- you've saved the day again!

Thanks

Greg :)

You are welcome !

I would like to note, that your programm works correctly when I compile it whit Delphi 7. May be your compiler has some bug that after exception can not calculate correctly StrToFloat(), I don't know ....
Emil.

I know that this thread already solved,but I made another solution.

program lengths;
uses crt;
var s:string[120];
begin
  write('please enter your length: ');
  readln(s);
  while (length(s) < 10) or (length(s) > 120) do begin
        write('please enter your length: ');
        readln(s);
  end;
  writeln('Ok.');
  readln;
end.
(*by FlamingClaw 2010.01.24.*)
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.