Slide 56 of 80
Notes:
- The previous “center” example had the size (or number of elements) of the array defined and thus hard-coded. This example is more general, in that, the size of the “array” is determined based on user input. In this case, when the user just presses <return>, meaning no more input, that defines the size of the array. These arrays can be very dynamic, limited only by the maximum number of variables supported by the CI. See thenotes from a few slides back for this discussion.
- To fix the infinite loop bug the variable “name” needs to be cleared or deleted inside the while loop. Recall that the INPUT command does not change the value of the variable if it times out or if the input value is null (“”). Thus we need to deletevar name each iteration, or set it to “”, or use the input() function. (Recall that the input() function returns an empty string, “”, if it times out or if the user just presses <return>. )
- Syntax: INPUT [NAME=]varname [[;PROMPT=]prompt ] [[;WAIT=]seconds ] [[;READCNT=]chars ] [[;DEFAULT=]default_str ] [;CONSOLE ]
- The variable, varname, will always be created by INPUT if it does not yet exist. Varname's value is typically the exact value entered as a response by the user; however, if the user enters no response (either by just pressing the enter key, or via the INPUT read expiring) varname's value is determined as follows:
- if a DEFAULT= value is provided that becomes the value for varname.
- if no DEFAULT= is specified and varname already exists it is not changed.
- if no DEFAULT= is specified and varname does not exist it is created with a value of "" (empty string).