This challenge challenges you to write a function that will take as its argument an array that specifies the types of input, an optional prompt, and a verification key; inputs and validates the values; and returns them.
Input types:
b - Boolean values.
c - Characters.
i - Integer values, rejects floating point values.
f - Floating point values, accepts integers.
n - Strings that only contain the characters a-z and A-Z, suitable for names.
s - Strings with any character type.
d - Dates in the form of dd/mm/yyyy, leap years and dates previous to 01/01/1900 can be rejected.
Examples:
b - Takes input as specified values, validates it, and returns "0" (false) or "1" (true).
c - Accepts any character.
i - Accepts any integer or number that, when the decimal is dropped, the resulting number is identical to the original number, such as 3 or 90.0. Rejects all other floating point values, such as 4.2 and 5.009.
f - Accepts any floating point or integer values. Floating point values include any number with a decimal value between .1 and .9. Numbers with .0 can count as a integer.
n - Accepts any string that contains only the characters a-z and A-Z. This input will accept "Name," but reject "Name1" and "My name."
s - Accepts any type of string.
d - Accepts any valid date of the form dd/mm/yyyy, such as 03/01/2014.
The function will take an array, vector, or other variable length type as an argument. Each element of the array will contain sub-elements of the input type request input type, an optional prompt, and for the input types s
, i
, and f
, an optional validation format. Input type b
must include a validation format in the form of "True/False". The array must be formatted as {type, prompt, validation}.
If no prompt is given, the function will use "> " as a prompt.
If a validation format for input type s
is given, it can contain any character that must be matched exactly and a request for any number of characters matching certain criterion. To request the string to have a specific number alphabetic characters (a-z and A-Z), use the format %(number of characters)(c)
, as in %6c
, which requests 6 characters. To request that the input be a certain number of single-digit integers, use %(number of digits)(i)
, as in %6i
, which requests six digits. Any other character given must be matched directly, where i
equals i
and -
equals -
". Use %%
to signify %
because %
alone is used to represent a command, as above. These rules can be combined, where i%6c-%%%2ixx
represents an input matching i
+ 6 characters + -%
+ 2 digits + xx
, where ifizlwi-%62xx
is a valid example.
Input validation formats for types i
and f
must be the in the form lowest/highest
, where lowest
is the lowest acceptable number and highest
is the highest acceptable number. Example: 6/50
would require a number between 6 and 50 (inclusive) to be input.
The function will return an array of strings that can be parsed by your program.
Return formatting:
b - Returns "0" (false) or "1" (true).
c, i, f, n, s, l, and d - returns input formatted as string.
Example call:
String s[][] = {{"n", "First name: "}
{"n", "Last name: "}
{"i", "Age: "}
{"d", "Date (dd/mm/yyyy): "}
{"s", "ISBN: ", "%1i-%6i-%2i-%1i"}
{"b", "Check out: ", "Yes/No"}};
String[] response = input(s);
The Boolean input will give the options as part of the prompt by displaying the input request as "(True/False)" after the prompt. The above example will display as
"Check out: (Yes/No)"
The function must continue requesting input until a valid input for each request is given.
No internet resources are allowed.
Any input-functions in any language that input and validate any type must not be used. E.g. if a language contains a function to input and validate an integer besides the traditional input function (System.in
, cin>>
, input()
, etc.), that function must not be used. You must do the validation yourself.
No trolling-style answers allowed. There are plenty of trolling questions for you to have fun with.
Shortest code code-golf that correctly inputs each of the specified types wins.
0
,false
,'false'
,''
,'off'
,'0'
all boolean values which evaluate asfalse
? \$\endgroup\$false
because all the values have to be returned in a string array. Otherwise, how could you return all the values? Use "1" fortrue
. \$\endgroup\$