Controlling user input? - Programmers Heaven

Howdy, Stranger!

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

Categories

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.

Controlling user input?

FuzzBallFuzzBall Posts: 1Member
Hi

I am wondering what the best way is of controlling user input?

If i want to read in someones age, how can i keep the input to within
0..9? and also keep the numbers entered to no more than 3?

thanks for any help.

Comments

  • sweeneysweeney Posts: 117Member
    : Hi
    :
    : I am wondering what the best way is of controlling user input?
    :
    : If i want to read in someones age, how can i keep the input to within
    : 0..9? and also keep the numbers entered to no more than 3?
    :
    : thanks for any help.
    :
    mmmmmm dont totaly understand but dose this help

    [code]

    program input;
    uses crt;

    const
    userID : char[3];

    begin

    write(' Please enter the user ID');
    readln(userID);

    if userID < 3 then
    write(' Sorry you have enterd to meany numbers');
    else
    'your program
    end.

    [code]

    This sould let the user input only 3 charicters from the keyboard, if its more then an error message will appear!

    Hope this helps a little!

    sweeney
  • zibadianzibadian Posts: 6,349Member
    : : Hi
    : :
    : : I am wondering what the best way is of controlling user input?
    : :
    : : If i want to read in someones age, how can i keep the input to within
    : : 0..9? and also keep the numbers entered to no more than 3?
    : :
    : : thanks for any help.
    : :
    : mmmmmm dont totaly understand but dose this help
    :
    : [code]
    :
    : program input;
    : uses crt;
    :
    : const
    : userID : char[3];
    :
    : begin
    :
    : write(' Please enter the user ID');
    : readln(userID);
    :
    : if userID < 3 then
    : write(' Sorry you have enterd to meany numbers');
    : else
    : 'your program
    : end.
    :
    : [/code]
    :
    : This sould let the user input only 3 charicters from the keyboard, if its more then an error message will appear!
    :
    : Hope this helps a little!
    :
    : sweeney
    :
    To check if the user entered only numbers, you need to use a for-do loop to check every singe character. Here is the code to do that:
    [code]
    IsValid := true; { Assume the UserID is valid }
    for i := 1 to Length(UserID) do
    if not (UserID[i] in ['0'..'9']) then { if a single char is different from a number }
    IsValid := false; { then invalidate the entry }
    [/code]
    You can also combine that with Sweeney's code and get the following:
    [code]
    IsValid := UserID <= 3; { Check the length }
    for i := 1 to Length(UserID) do { Check the characters }
    if not (UserID[i] in ['0'..'9']) then
    IsValid := false;
    if IsValid then begin
    { Your program code }
    end else
    { Some error message }
    [/code]
    You could also repeat asking the user for a valid age:
    [code]
    repeat
    write('Enter your age: ');
    readln(UserID);
    IsValid := UserID <= 3; { Check the length }
    for i := 1 to Length(UserID) do { Check the characters }
    if not (UserID[i] in ['0'..'9']) then
    IsValid := false;
    if not IsValid then
    { Some error message }
    until IsValid;
    { Rest of program }
    [/code]
  • ManningManning Posts: 1,621Member
    : You could also repeat asking the user for a valid age:
    : [code]
    : repeat
    : write('Enter your age: ');
    : readln(UserID);
    : IsValid := [Red]Length([/Red]UserID[Red])[/Red] <= 3; { Check the length }
    : for i := 1 to Length(UserID) do { Check the characters }
    : if not (UserID[i] in ['0'..'9']) then
    : IsValid := false;
    : if not IsValid then
    : { Some error message }
    : until IsValid;
    : { Rest of program }
    : [/code]

    Just fixed the typo
  • zibadianzibadian Posts: 6,349Member
    : : You could also repeat asking the user for a valid age:
    : : [code]
    : : repeat
    : : write('Enter your age: ');
    : : readln(UserID);
    : : IsValid := [Red]Length([/Red]UserID[Red])[/Red] <= 3; { Check the length }
    : : for i := 1 to Length(UserID) do { Check the characters }
    : : if not (UserID[i] in ['0'..'9']) then
    : : IsValid := false;
    : : if not IsValid then
    : : { Some error message }
    : : until IsValid;
    : : { Rest of program }
    : : [/code]
    :
    : Just fixed the typo
    :
    You're right, thanks.
Sign In or Register to comment.