How to read Data from a standard input stream console in Nim?

Nim language provides a Stdin library to read input data from a user.

This tutorial explains multiple ways to read input data from a user and solve the below questions. How to read from the stdin with the nim script? How to get input from the console in Nim?

Nim How to read input data from a user?

The readLine procedure works with stdin. stdin is a variable system module that refers standard input stream such as a file or console.

readLine(stdin) or stdin.readLine() both functions read data from a console and assign it to a variable. It reads a single line of data from

These methods always read the data as a string type. readLine(stdin) stops the program execution and waits for user input until enter is pressed.

echo "Enter Name:"
let name = readLine(stdin)

echo "User entered string: ", name
Enter Name:
Eric
User entered string: Eric

How to read user input as a number in Nim with an example

readLine() method reads data from the standard input stream as a string.

Convert string into integer using the parseInt() method Finally, Print the integer to console Here is an example

import strutils

echo "Enter Age:"
let age = readLine(stdin).parseInt()
echo "Age is ", age

Output:

Enter Age:
22
Age is : 22