Introduction Welcome Getting Started My First Program     Troubleshooting and Distributing Transforming Data Interactive Programs     Programming Exercises     Making Games
Documentation Declarations     Procedures     Variables Statements     Flow Control Expressions Libraries     Print     IO     Random     Game Engine
Tutorials Complete Tutorials     Introduction to Programming     Quick Start     Programming Exercises     Programming Games

LIBRARY: IO


All About the IO Library in Vizzcode





About the IO Library

The IO library allows you to display values to and read values from the command console.
IO means Input/Output and represents that this library contains procedures for both reading and writing to the user.

Just like the Print library, the IO library is made for programs that are console applications (programs in which you didn't import the Game Engine library).
The entire Print library is inside the IO so you don't need (and in fact, aren't allowed to - because of identifier redefinition) import both libraries.
With the IO library (besides being able to print all the types like in the Print library), you can also read (or scan) user input. This works for both text input (anything the user types) and ints (whole numbers) the user types.




How to Use It

All you need to do is import the IO library:

#import
"IO"

Importing the IO library

You can then just call its procedures anywhere in the code. For example:
#import
"IO"


main
:: () {
    name :=
scan
(
"What's your name?"
);
    
print
(
"Hello, "
);
    
print
(name);

    number :=
scan_int
(
"Type a number: "
);
    
print
(
"That number squared is: "
);
    
print_int
(number * number);
}

Example of the IO library





Complete API

scan

This is the declaration of the scan procedure:
scan
:: (message :=
""
) -> value :
string
{

This procedure prints the message, it receives as parameter, to the user and waits for user input.
The users can type whatever they like and must then press ENTER to send the message.
The message the user typed is returned by the scan procedures as the string parameter value.

You can call it passing a string value or just use the default value (empty string - meaning no message):
#import
"IO"


main
:: () {
    input :=
scan
(
"Enter a message: "
);
}

How to use the scan procedure

#import
"IO"


main
:: () {
    input :=
scan
(
"Enter a message: "
);

    
print
(
"You typed: "
);
    
print
(input);
}

Reading a message from the user



scan_int

This is the declaration of the scan_int procedure:
scan_int
:: (message :=
""
) -> value :
string
, success :
bool
{

This procedure prints the message it receives as parameter to the user and waits for user input.
The users type some text and then press ENTER to send the it.
If the text is a valid integer number, the procedure returns the typed number as an int in the variable value, and returns success as true.
If the text is not a valid int, the procedure returns the value zero and success as false.
You can get more information on reading multiple return values with compound declarations.

You can call it passing a string value or just use the default value (empty string - meaning no message):
#import
"IO"


main
:: () {
    number, success :=
scan_int
(
"Enter a number: "
);
}

Reading a number from the user

#import
"IO"


main
:: () {
    number, success :=
scan_int
(
"Enter a number: "
);

    
if
success {
        
print
(
"You typed a valid number: "
);
        
print_int
(number);
    }
else
print
(
"You typed an invalid number!"
);
}

Using scan_int safely



print

This is the declaration of the print procedure:
print
:: (message :=
"Hello!\n"
) {

It works just like in the Print library.
You can call it passing a string value or just use the default value:
#import
"IO"


main
:: () {
    
print
();
    
print
(
"Custom message
);
    var :
string
=
"Variable"
;
    
print
(variable);
}

How to use the print procedure



print_int

This is the declaration of the print_int procedure:
print_int
:: (value :
int
) {

It works just like in the Print library.
You can call it passing a int value:
#import
"IO"


main
:: () {
    
print_int
(
123
);
    
print_int
(
456
+
789
);
    var :
int
=
10
;
    
print_int
(var);
}

How display numbers in Vizzcode



print_float

This is the declaration of the print_float procedure:
print_float
:: (value :
float
) {

It works just like in the Print library.
You can call it passing a float value:
#import
"Print"


main
:: () {
    
print_float
(
123
);
    
print_float
(
123.456
);
    
print_float
(
11.22
+
33.44
);
    var :
float
=
10
;
    
print_float
(var);
}

How does the print float procedure work in Vizzcode



print_bool

This is the declaration of the print_bool procedure:
print_bool
:: (value :
bool
) {

It works just like in the Print library.
You can call it passing a bool value:
#import
"IO"


main
:: () {
    
print_bool
(
true
);
    
print_bool
(
100
<
56
);
    var :
bool
=
"Wolf"
!=
"Lion"
;
    
print_bool
(var);
}

Using the print bool procedure