I’m using Dev-Pas 1.9.2 and am trying to make sure the program doesn’t crash when a symbol or a letter value is entered.
I’ve googled and googled and can’t find any resoruce on how to achieve this.
Any help is greatly appreciated. Thanks!
Here is the code I’m trying to manage the input:
Function GetMenuChoice : Integer;
Var
OptionChosen : Integer;
Begin
Write('Please enter your choice: ');
Readln(OptionChosen);
If (OptionChosen < 1) Or ((OptionChosen > 4) And (OptionChosen <> 9))
Then
Begin
Writeln;
Writeln('That was not one of the allowed options. Please try again: ');
End;
GetMenuChoice := OptionChosen;
End;
Change your code to accept a Char instead; if you need an integer for some reason, handle the conversion afterward.
This works in Delphi; unless you can’t use sets like
['1'..'4','9']and set operators, it should work fine.If you absolutely need a number to be returned, change the return type back to integer (or byte) and then change the final line to:
or