evolve
Input and Output
Input and output
Output to the screen
To send output to the screen the out command is used you'll already have seen this used in the preceding examples. If you want to display something on the screen followed by a line feed then use outl. Multiple strings and variables can be placed on the same line separated by a white space character.
//---------------------------------------
// File: ScreenOutput.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
//assign different Variable types
BoolVar = true
ByteVar = 0xff
IntVar = 456
FloatVar = 0.565
StrVar = "test"
outl "Screen Output"
new_line
//display the Variables
out "Display byte: "
outl ByteVar
outl "Display bool: " BoolVar
outl "Display an int: " IntVar " then a float: " FloatVar
out "Display a string: "
outl StrVar
_block
In the above example You will also have noticed the command new_line has been used this adds a line feed.
There is a version of the out function that enables the printing of list variables (see 26) this command is coutlst it has two arguments the variable list to display and a delimiter string for example if you want to display a list variable separated by spaces then you would use
outlst Test ", "
If you want to print each element on a new line then use the constant #LF.
outlst Test #LF
The following shows this function in action.
//---------------------------------------
// File: OutList.e
//---------------------------------------
//---------------------------------------
// Block: Int
//---------------------------------------
block Int
I = 0
while I < 10
Test[I] = I
++ I
_while
//print list comma separated
outlst Test ", "
new_line
//print a list with line feeds
outlst Test #LF
new_line
_block
//---------------------------------------
// Block: Bool
//---------------------------------------
block Bool
Test[0] = false
Test[1] = true
//print list comma separated
outlst Test ", "
new_line
//print a list with line feeds
outlst Test #LF
new_line
_block
//---------------------------------------
// Block: Byte
//---------------------------------------
block Byte
Test[0] = 0x01
Test[1] = 0xff
//print list comma separated
outlst Test ", "
new_line
//print a list with line feeds
outlst Test #LF
new_line
_block
//---------------------------------------
// Block: Float
//---------------------------------------
block Float
Test[0] = 1.2
Test[1] = 1.3
Test[2] = 1.4
//print list comma separated
outlst Test " : "
new_line
//print a list with line feeds
outlst Test #LF
new_line
_block
//---------------------------------------
// Block: String
//---------------------------------------
block String
Test[0] = "abcde"
Test[1] = "efgh"
Test[2] = "ijkl"
//print list comma separated
outlst Test ""
new_line
//print a list with line feeds
outlst Test #LF
new_line
_block
//---------------------------------------
// Block: Go
//---------------------------------------
block Go
Int
Bool
Byte
Float
String
_block
Formatting Output
The information sent to the screen can be formated to allow for different alignment and number formats.
Alignment
The alignment of output can be set using the out_format command this allows specification of the width, a fill character and the type of alignment.
The alignment can be set to one of the following
#RIGHT This constant sets the text to be right justified at the specified width.
#LEFT This constant sets the text to be left justified.
#INTERNAL This constant pads out a numeric value to fill the field with spaces between the first character and any sign or base character.
The following code will produce the output
--10-right
…..........20-right
-xxxxxxx23
10-internal
- 244
20-internal
0-left
//---------------------------------------
// File: ScreenFormattedOutput.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
out_format 10 '-' #RIGHT
outl "10-right"
out_format 20 '.' #RIGHT
outl "20-right"
out_format 10 'x' #INTERNAL
outl -23
outl "10-internal"
out_format 20 ' ' #INTERNAL
outl -244
outl "20-internal"
out_format 0 ' ' #LEFT
outl "0-left"
_block
Integer Data Formatting
When an integer or byte value is sent to the screen it can also be formatted with out_ibase format command.
The output can be set to one of the following.
#HEX any integer or byte variable will be displayed in a hexadecimal format.
#OCT any integer or byte variable will be displayed in octal format.
#DEC any integers or byte variable will displayed in decimal format.
Floating Point Formatting
The precision of decimal point output is set with the out_fprec command and the base can be set with the out_fbase command.
The precision sets the number of decimal places that are displayed.
The out_fbase command sets the base and format of the output
#DEFAULTFLOAT any float will be displayed in the default format.
#FIXED any float will be displayed in a fixed point format.
#SCIENTIFIC any float will be displayed in a scientific format using exponents.
#HEXFLOAT any float will be displayed in a hexadecimal format using exponents.
The following code demonstrates all the formatting options.
//---------------------------------------
// File: ScreenVariableFormat.e
//---------------------------------------
//---------------------------------------
// Block: NumberFormatCheck
//---------------------------------------
block FloatPrecisionCheck
Value1 = 1235.653
outl "Default: " Value1
out_fprec 1
outl "Precision 1: " Value1
out_fprec 2
outl "Precision 2: " Value1
out_fprec 7
outl "Precision 7: " Value1
_block
//---------------------------------------
// Block: FloatFormatCheck
//---------------------------------------
block FloatFormatCheck
Value1 = 1235.653
out_fbase #DEFAULTFLOAT
outl "DEFAULTFLOAT: " Value1
out_fbase #FIXED
outl "FIXED: " Value1
out_fbase #SCIENTIFIC
outl "SCIENTIFIC: " Value1
out_fbase #HEXFLOAT
outl "HEXFLOAT: " Value1
out_fbase #DEFAULTFLOAT
_block
//---------------------------------------
// Block: IntFormatCheck
//---------------------------------------
block IntFormatCheck
Value = 123456
out_ibase #DEC
outl "DEC: " Value
out_ibase #HEX
outl "HEX: " Value
out_ibase #OCT
outl "OCT: " Value
out_ibase #DEC
_block
//---------------------------------------
// Block: go
//---------------------------------------
block Go
FloatPrecisionCheck
new_line
FloatFormatCheck
new_line
IntFormatCheck
new_line
_block
Keyboard Input
There are three commands which allow input from the key board.
Reading Keyboard Input
The first command we will look at is the in command which allows text or numbers to be entered.
//---------------------------------------
// File: KeyBoardInput.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
//Enter a string value
InputStrValue = "
out "Enter a string value: "
in InputStrValue
outl "You entered: " InputStrValue
new_line
//Enter a numeric value
InputNumericValue = 0
out "Enter a numeric value: "
in InputNumericValue
outl "You entered: " InputNumericValue
new_line
//Enter another value
out "Enter another value: "
in DefineValue
outl "You entered: " DefineValue
new_line
_block
The above code defines a string variable asks the user to input a string and then displays it.
It then repeats the process but with a numeric variable. If you try the code you will find that in this instance if you don't enter a numeric value then an error will be displayed and you will be asked to enter the value again.
In the last case the variable is not predefined so evolve creates it and assumes its a string.
Reading a Keyboard key
There are two command that can be used to get single characters from the key board they are key_in and key_inwait.
The difference is that the command key_in will read the keyboard and carry on while the key_inwait command will wait for a key to be pressed before continuing with the program.
Both commands will return the ASCII value of the button pressed if a numeric variable is passed into them or ASCII value if a string variable is passed into them. The variable is not predefined then a byte variable will be created and the value passed into this.
//---------------------------------------
// File: KeyInput.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
//use a byte input
ByteInput = 0x00
//wait until a key is pressed
while ByteInput == 0
key_in ByteInput
_while
//display the key
outl "You typed :" ByteInput
//use a string input
StringInput = ""
while StringInput == ""
key_in StringInput
_while
//use a string input
outl "You typed :" StringInput
key_inwait ByteInput
//use a byte input
outl "You typed :" ByteInput
key_inwait StringInput
outl "You typed :" StringInput
_block