Getting Input

You can make bad choices with data while you’re programming, but it’s even more likely that users will put incorrect data into your programs when asked for input. Creating resilient code means taking special care with user input. In the following section, your program reads input, and you’ll learn to make it withstand runtime errors.

To gain a better understanding of how Crystal works with types, let’s build a program to read in currencies and their relative exchange rates. Let’s start with reading in integer numbers into an array. You can find the complete source code in getting_input.cr.

The console lets you collect input, but the Playground doesn’t. To make this work, you have to open up a terminal and type: crystal getting_input.cr ...

Get Programming Crystal now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.