Skip to content

Reading Input

Reading Input

Thagore provides built-in and standard library functions for reading from stdin. The built-in input() function reads a line from the console, and the io module adds prompts and bulk-read helpers.


Built-in: input()

The input() function is a language built-in — no import needed. It reads one line from standard input and returns it as a String. The trailing newline is stripped automatically.

hello_input.tg
func main() -> i32:
print("What is your name?")
let name = input()
print("Hello, " + name + "!")
return 0
What is your name?
> Minh
Hello, Minh!

Built-in: input() with i32 parse

To read an integer, read the string first then parse with str.parse_i32(), or use io.input_i32() from the io module:

read_number.tg
import "lib/io.tg" as io
func main() -> i32:
print("Enter a number:")
let n = io.input_i32()
print(n * 2)
return 0
Enter a number:
> 21
42

io Module Functions

Import the io module for all input helpers:

import "lib/io.tg" as io

Function Reference

FunctionReturnDescription
io.input_prompt(prompt)StringPrint a prompt then read a line from stdin
io.input_prompt()StringRead a line with no prompt (same as input())
io.input_i32()i32Read a line and parse it as a signed integer
io.stdin_read_line(prompt)StringAlias for input_prompt
io.stdin_read_all()StringRead entire stdin into a single string
io.stdread(path)StringRead from a file or from stdin if path is "", "-", or "stdin"

io.input_prompt() — Prompt + Read

Shows a prompt on the same line, then waits for input:

greet.tg
import "lib/io.tg" as io
import "lib/string.tg" as str
func main() -> i32:
let name = str.trim(io.input_prompt("Enter your name: "))
let age_s = str.trim(io.input_prompt("Enter your age: "))
print("Hello " + name + "! You entered age: " + age_s)
return 0
Enter your name: Minh
Enter your age: 22
Hello Minh! You entered age: 22

io.stdin_read_all() — Read All of Stdin

Reads everything from stdin into a single String. Useful when piping input into a program:

echo_all.tg
import "lib/io.tg" as io
func main() -> i32:
let text = io.stdin_read_all()
print("You sent " + text)
return 0
Terminal window
echo "hello world" | ./echo_all
# You sent hello world

io.stdread() — File or Stdin

stdread(path) reads from a file path, or from stdin if the path is one of the standard stdin selectors:

Path valueBehaviour
"" (empty string)reads stdin
"-"reads stdin
"stdin" / "STDIN"reads stdin
"con" / "CON"reads stdin (Windows console)
"/dev/stdin"reads stdin
any other pathreads that file
cat.tg
import "lib/io.tg" as io
import "lib/env.tg" as env
func main() -> i32:
let path = env.get(1)
let text = io.stdread(path)
print(text)
return 0
Terminal window
./cat myfile.txt # reads myfile.txt
./cat - # reads stdin
./cat # reads stdin (empty path)

Reading Multiple Lines (Loop)

Use a loop with input() to read until EOF:

sum_lines.tg
import "lib/string.tg" as str
func main() -> i32:
let total = 0
let line = input()
while (str.len(line) > 0):
let n = str.parse_i32(line)
total = total + n
line = input()
print(total)
return 0
Terminal window
printf "10\n20\n30\n" | ./sum_lines
# 60

Practical Example: Simple Calculator

calc.tg
import "lib/io.tg" as io
import "lib/string.tg" as str
func main() -> i32:
let a = str.parse_i32(str.trim(io.input_prompt("First number: ")))
let b = str.parse_i32(str.trim(io.input_prompt("Second number: ")))
print("Sum: " + str.from_i32(a + b))
print("Diff: " + str.from_i32(a - b))
print("Product: " + str.from_i32(a * b))
return 0
First number: 10
Second number: 3
Sum: 13
Diff: 7
Product: 30

Summary

Use caseCode
Read a string line (no prompt)let s = input()
Read a string with promptlet s = io.input_prompt("Enter: ")
Read an integerlet n = io.input_i32()
Read all of stdinlet text = io.stdin_read_all()
Read file or stdinlet text = io.stdread(path)