A Beginner's Guide to D/The Basics/Basic Input

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] Introduction

The built-in capabilities of D (meaning the capabilities of the runtime library Phobos) for input from a console (alias terminal or DOS-Box) are very restricted.

Here is a simple program that demonstrates string input with din.readLine() which reads a complete line of input. A line means all characters typed until the user pressed RETURN on keyboard. It doesn't mean a physical line in the console-window which is usually 80 characters wide.

 import std.cstream; /* for din.readLine() */
 import std.stdio;   /* for writef()/writefln() */
 
 void main() 
 {
   char[] name;
 
   writef("Hello friend. Please enter your name: ");
   name = din.readLine();
 
   writefln("Thanks, %s", name);
 }