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

INTERACTIVE PROGRAMS


Create programs that people can interact with





Reading Input from the Command Line

We have learned how to use the Print library to display messages and numbers so now I will show you another library to help us with the last part of the puzzle: user input.
Just like importing Print, we need to create an Import node. But this time, we will import a library called IO.

How to import the IO library

IO stands for Input/Output (you may see this being used around). Besides also including everything in the Print library, this library contains two new procedures: scan and scan_int. These procedures take an optional message (that will print to the screen just like a normal print call) and return the value the user typed.
Just like with arithmetic operators, we have to assign the return value to a variable so it's stored for later use.
Like this:

How to read user input in Vizzcode

Now the data the user typed is stored in a variable and available to us. We can easily print the value of the variable.
#import
"IO"


main
:: () {
    var :=
scan
(
"Type a nice message: "
);
    
print
(var);
}

Note: the scan procedures reads whatever text the user typed. So its return value is of type string. (We already talked about variable types, and you can see a lot more about it here). You can read specifically about the string type used to store text here.

Just like scan can be used to read the text the user typed, scan_int can read the numbers.
Now we will store an int variable and display it with print_int. This integer variable can be operated on just like we learned before.

How to read numbers from the user

With what you already learned, I think you can tackle that read-two-numbers-and-add-them-together program we talked about a while back. What do you think?
You already know how to read numbers, add them and display the result.

Once you give it your best shot, continue on to the next section. :)




Making a Simple Calculator

Let's make a simple calculator: a program that reads two numbers, add them together and show the result.
To read and store two numbers that the user typed you need to declare two variables and assign them with the result of the scan_int call.

How to make a calculator in Vizzcode

Then, you can declare another variable to store the result of the addition and display it:

How to add two numbers in Vizzcode

Or simply print the addition expression directly:

How to add two numbers in Vizzcode without declaring a variable

Pretty simple, right?
If you have any trouble understanding a specific part, don't be afraid to re-read the documentation pages or try out the tutorials inside Vizzcode.





Having Fun with User Input

With the ability to read text and numbers, operate on this data and display messages and numbers, you can create all sorts of interesting programs!

How about a program that calculates the age of the user:
Enter the year you were born: 1996
You are 24 years old! (or 23 if you haven't had birthday this year yet... :P )

This program uses some cool arithmetic expressions to display how many hours, minutes and seconds, that the amount of seconds the user typed corresponds to:
Enter a number of seconds: 3812
This correspond to: 1h 3m 32s

This other program knows if the number you typed is even or odd:
Type a number: 7837
Even or Odd (1=even, 0=odd): 0

Try programming these (and other programs you come up with) to put to use what you have been learning. :)

As you can see, you can use your imagination and the tools you already learned to come up with and create some very cool and interesting programs!
But there is still much to learn! At the end of this section I will show you a place full of interesting problems and new concepts for you to learn and experiment with!




Error Handling

We have already talked about fixing problems you encounter when developing your programs. But there is another class of errors that are really important and deserve some attention.

Whenever we create an interactive program, there is a possibility that the user will not use the program as intended. Either because of a misunderstanding of what the program expects (like "I didn't know I was supposed to write '0.0' instead of '0'"), a simple human error (like a typo), an antagonistic user (someone that tries to find the "breaking-point" of the interaction - either an hacker that wants to exploit your program, or a video game player exploring the physics of the world, for example) and other cases.

In these cases, the program has to gracefully handle the unexpected cases - making them simply expected (but undesirable) conditions.

For example, what happens if your program asks for a number but the user types some random text?
What happens if your program divides two numbers entered by the user and the divisor turns out to be zero?
If your program reads a certain file, what happens if the file is not like you expected, or blank or doesn't exist?

Does your program crash? Does it freeze? Does it stop working as intended? Does it display garbage to the user?
You certainly have used a program that acted totally unexpectedly because it wanted the input a certain way.

As programmers it is our duty to test these corner cases of user input, trying to cover all the input possibility space.
The more interactive a program (like a video game), the bigger the possibility space, allowing to user to participate more in the outcome of the program (which is great!) but making it harder to predict and implement those input corner-cases.

Developing this skill is very important: thinking about what the user can do to break the program and handle that case correctly.
In the Programming Exercises there are several examples, tips and exercises to make the unpredictable, program-breaking cases be simply another expected use case. :)
You will learn all about how to handle user "errors" in those exercises. For now, try thinking about what the user can do to create unexpected behaviours in your programs.




More Interactive Programs

We just learned how to read input from the command line and created several programs playing around with it.
However you may want to make a more interactive program, one where the user can click, drag, type, import files, ... all sorts of stuff. As we already discussed, there are two types of programs: console programs (that launches in that boring terminal) and program that uses a window of its own. Most of the programs you interact with everyday are window programs: your browser, a video or audio player, video games and of course Vizzcode!

In Vizzcode, it's really easy to start creating window-based programs, particularly games. Later on, you will learn all about Making Games using Vizzcode!




Taking Your Own Steps

Like any apprentice, there comes a point where you have to learn to walk with your own legs and explore the world on your own - following your particular needs and desires. (Besides, I've been talking for a long time and
I'm
al
mo
st
out
of
v
o
i
c
e
).


To help you progress without help, I have prepared a hub of programming exercises, challenges and program ideas for you to explore. It's full of hints on how to solve them, as well as links to the other useful pages of the documentation.
There, you will be able to learn several new skills to help you develop the programs you want, all the way the complex projects!
The embedded tutorials are also an excelent way to expand your skill set!

That's it, then. It's hard to say goodbye, but hopefully you will be well accompanied as your forge your own path in this great world of creation.
I'll see you around... programmer. :)