Recent

Author Topic: Validating user input  (Read 482 times)

dukester

  • New Member
  • *
  • Posts: 13
Validating user input
« on: May 16, 2022, 04:34:01 am »
newbie here. Writing a console-based, menu-driven script.
I need to validate the user input and restrict it to: a or b or q.

I've got a working script but without input checks. I can't seem to stumble on the correct validation logic. Pointers please. TIA ..

440bx

  • Hero Member
  • *****
  • Posts: 2831
Re: Validating user input
« Reply #1 on: May 16, 2022, 06:43:05 am »
newbie here. Writing a console-based, menu-driven script.
I need to validate the user input and restrict it to: a or b or q.

I've got a working script but without input checks. I can't seem to stumble on the correct validation logic. Pointers please. TIA ..
What do you mean by "script" ?... a Pascal program or something else ?  ... can you post the "working script" you currently have ? (that would be particularly helpful)


FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 11627
Re: Validating user input
« Reply #2 on: May 16, 2022, 07:54:16 am »
A shell scrpt.
Black themes should be banned.

dukester

  • New Member
  • *
  • Posts: 13
Re: Validating user input
« Reply #3 on: May 16, 2022, 08:07:36 pm »
What do you mean by "script" ?... a Pascal program or something else ?  ... can you post the "working script" you currently have ? (that would be particularly helpful)

I should have said program instead of script! I just need general logic on how to restrict user input to certain characters AND validate that the input is correct.

Thaddy

  • Hero Member
  • *****
  • Posts: 11627
Re: Validating user input
« Reply #4 on: May 16, 2022, 08:58:28 pm »
Black themes should be banned.

Handoko

  • Hero Member
  • *****
  • Posts: 4593
  • My goal: build my own game engine using Lazarus
Re: Validating user input
« Reply #5 on: May 16, 2022, 09:29:55 pm »
Doing menu in console mode, I will do it like this:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   Crt;
  5.  
  6. procedure WriteText(X, Y, C: Byte; const S: string);
  7. begin
  8.   GotoXY(X, Y);
  9.   TextColor(C);
  10.   WriteLn(S);
  11. end;
  12.  
  13. procedure About;
  14. begin
  15.   ClrScr;
  16.   WriteText(1, 1, 3, 'This is a demo showing how to do menu in console mode');
  17.   WriteText(1, 2, 3, 'in Pascal using Lazarus/FPC.');
  18.   repeat until KeyPressed;
  19.   ReadKey;
  20. end;
  21.  
  22. procedure Quote;
  23. begin
  24.   ClrScr;
  25.   case Random(5) of
  26.     0: begin
  27.          WriteText(1, 1, 2, '"The price of greatness is responsibility."');
  28.          WriteText(1, 2, 1, '(Sir Winston Churchill)');
  29.        end;
  30.     1: begin
  31.          WriteText(1, 1, 2, '"Whatever you are, be a good one"');
  32.          WriteText(1, 2, 1, '(Abraham Lincoln)');
  33.        end;
  34.     2: begin
  35.          WriteText(1, 1, 2, '"Everything you can imagine is real."');
  36.          WriteText(1, 2, 1, '(Pablo Picasso)');
  37.        end;
  38.     3: begin
  39.          WriteText(1, 1, 2, '"Never regret anything that made you smile."');
  40.          WriteText(1, 2, 1, '(Mark Twain)');
  41.        end;
  42.     4: begin
  43.          WriteText(1, 1, 2, '"Love is a serious mental disease."');
  44.          WriteText(1, 2, 1, '(Plato)');
  45.        end;
  46.   end;
  47.   repeat until KeyPressed;
  48.   ReadKey;
  49. end;
  50.  
  51. var
  52.   C: Char;
  53. begin
  54.   repeat
  55.     ClrScr;
  56.     WriteText(1,  3, 4, 'What do you want to do?');
  57.     WriteText(3,  5, 3, '[a] - About this demo');
  58.     WriteText(3,  7, 3, '[b] - Random quote');
  59.     WriteText(3,  9, 3, '[q] - Quit');
  60.     WriteText(1, 11, 7, 'Please type [a], [b] or [q]:');
  61.     C := LowerCase(ReadKey);
  62.     case C of
  63.       'a': About;
  64.       'b': Quote;
  65.     end;
  66.   until C = 'q';
  67.   ClrScr;
  68.   WriteText(1, 1, 7, 'Goodbye ...');
  69. end.

dukester

  • New Member
  • *
  • Posts: 13
Re: Validating user input
« Reply #6 on: May 16, 2022, 10:10:25 pm »
@Thaddy

CharInSet will do the validation that I want. Thx

@Handoko

Thanks for the code! That is fairly close to what I had come up with. However, I wanted to include some checks for invalid input so that the program would not crash/core dump. I'm on a Linux box:
 fpc -v
Free Pascal Compiler version 3.2.0

My project is just a simple learning exercise. Thanks again!

dukester

  • New Member
  • *
  • Posts: 13
Re: Validating user input
« Reply #7 on: May 16, 2022, 10:43:46 pm »
@Handoko
Just reviewed your excellent code. Thx. The case statement does exactly what I want. Nothing can get through but what the case statement says is OK. Excellent!

 

TinyPortal © 2005-2018