Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Problem with "readln" statement

kaptakapta Posts: 5Member
basically what i want to do is have a limited field.
if i have a string[5] i should only be able to enter 5 characters in my
readln, but i can enter more than that. is there a way to limit it so u
can only type in 5 characters?

Comments

  • steveXPsteveXP Posts: 22Member
    : basically what i want to do is have a limited field.
    : if i have a string[5] i should only be able to enter 5 characters in my
    : readln, but i can enter more than that. is there a way to limit it so u
    : can only type in 5 characters?
    :
    Hey there. There is also another way of reading input from the keyboard. You can use Repeat Until Keypressed and then obtain the character pressed by assigning it to a variable (Press:=Readkey;). These commands are held in the Crt unit ;o) Hope that helps ya out.

    [code]
    Repeat
    Repeat Until Keypressed; {Wait for keypress}
    Press:=Readkey; {Obtain character}
    Write(Press);
    Str:=Str+Press; {Add character to String}
    Until length(Str)=5;
    [/code]

    -SteveXP- o(-_-)o

  • ManningManning Posts: 1,621Member
    : Hey there. There is also another way of reading input from the keyboard. You can use Repeat Until Keypressed and then obtain the character pressed by assigning it to a variable (Press:=Readkey;). These commands are held in the Crt unit ;o) Hope that helps ya out.
    :
    : [code]
    : Repeat
    : Repeat Until Keypressed; {Wait for keypress}
    : Press:=Readkey; {Obtain character}
    : Write(Press);
    : Str:=Str+Press; {Add character to String}
    : Until length(Str)=5;
    : [/code]

    ReadKey is a blocking function, so you dont need that extra repeat until keypressed loop in there. It is actually a good idea to have it in conjunction with timeslice code though, since that'll stop your program from using 100% of the CPU.
  • roland48roland48 Posts: 23Member
    : : Hey there. There is also another way of reading input from the keyboard. You can use Repeat Until Keypressed and then obtain the character pressed by assigning it to a variable (Press:=Readkey;). These commands are held in the Crt unit ;o) Hope that helps ya out.
    : :
    : : [code]
    : : Repeat
    : : Repeat Until Keypressed; {Wait for keypress}
    : : Press:=Readkey; {Obtain character}
    : : Write(Press);
    : : Str:=Str+Press; {Add character to String}
    : : Until length(Str)=5;
    : : [/code]
    :
    : ReadKey is a blocking function, so you dont need that extra repeat until keypressed loop in there. It is actually a good idea to have it
    in conjunction with timeslice code though, since that'll stop your program from using 100% of the CPU.
    :
    I think it doesn't work in this easy way, becouse you have to check
    if the key is a valid character and the user must have a chance to edit his string.



  • Phat NatPhat Nat Posts: 757Member
    : basically what i want to do is have a limited field.
    : if i have a string[5] i should only be able to enter 5 characters in my
    : readln, but i can enter more than that. is there a way to limit it so u
    : can only type in 5 characters?
    :

    This will work for you for any desired length:

    [code]
    FUNCTION GetString( MaxLength : Byte) : String;
    VAR
    Key : Char;
    S : String;
    Begin
    Repeat
    Key := Readkey;
    If Key = #8 Then
    Begin
    If Length(S) > 0 Then Dec(S[0]);
    End
    ELSE
    If (Length(S) <= MaxLength) AND { Make sure it's not too long }
    Not(Key In[#10,#13,#27]) Then { Make sure it doesn't }
    S := S + Key; { include Enter & ESC }
    Until (Key = #13) or (Key = #27);
    If Key = #13 Then GetString := S
    ELSE GetString := '';
    End;
    [/code]

    This will do any string length up to 255. Below is code for a maximum length of 5 charaters.

    [code]
    S := GetString(5);
    [/code]

    Phat Nat

Sign In or Register to comment.