Getting Started with Smart Console Applications
Introduction
Smart console applications are a little bit different from the conventional ones. In order to input data, the user first presses the Execute button:

Instructions

Input
Our first example shows how you can use an incrementing variable to match each input with the appropriate code to process it. The other examples show how you can nest routines inside the ProcessCommand and/or PopulateConsole procedures, which is useful when adapting code from existing Pascal console programs. These three examples will compile with Version 3.0 of Smart Mobile Studio.
An example using different types of input
unit uInputDemo; interface uses // w3system, w3scroll, w3console, w3components, w3application, w3consoleapp, w3lists; System.Types, System.Lists, SmartCL.System, SmartCL.Scroll, SmartCL.Console, SmartCL.Components, SmartCL.Application, SmartCL.ConsoleApp; type TApplication = Class(TW3CustomConsoleApplication) private i : integer := 0; protected procedure PopulateConsole; override; procedure ProcessCommand(aCommand: string); override; end; implementation procedure TApplication.PopulateConsole; begin Console.WriteLn('Press the Execute button (or type Tab then Enter) to answer questions.'); Console.WriteLn(''); Console.WriteLn('What is your name?'); end; procedure TApplication.ProcessCommand(aCommand: string); var age : integer; begin inc(i); case i of 1 : begin Console.writeln('Hello, ' + aCommand + '!'); Console.writeln('How old are you?'); end; 2 : begin age := StrToInt(aCommand); Console.writeln ('Next year you will be ' + IntToStr(age + 1) + '.'); end; end; end; end.

Console after entering Andy then 26
An example using a nested function
This example shows how you can use code from an existing, conventional console application (in this case from our tutorial on recursion).unit uFactorialDemo; interface uses // w3system, w3scroll, w3console, w3components, w3application, w3consoleapp, w3lists; System.Types, System.Lists, SmartCL.System, SmartCL.Scroll, SmartCL.Console, SmartCL.Components, SmartCL.Application, SmartCL.ConsoleApp; type TApplication = class(TW3CustomConsoleApplication) private protected procedure PopulateConsole; override; procedure ProcessCommand(aCommand: string); override; end; implementation procedure TApplication.PopulateConsole; begin Console.WriteLn('Please enter an integer from 0 to 12. '); end; procedure TApplication.ProcessCommand(aCommand: string); var Num : integer; function Factorial(i : integer) : integer; begin if i = 0 then result := 1 else result := i * Factorial(i - 1); end; begin Num := StrToInt(aCommand); Console.WriteLn(IntToStr(Num) + '! = ' + IntToStr(Factorial(Num))); Console.WriteLn('Please enter an integer from 0 to 12. '); end; end.
This screenshot shows the entry of the fourth item of data.

Data entry
An example with no input
This example uses logic from one of our TINY demonstrations. It draws a Pascal triangle as shown in this web page. Note that we add non-breaking spaces ( ) to separate values. (Otherwise, the HTML will show consecutive space characters as a single space). The Console has a WriteLn method but not a Write method, so we build up each current line before writing.
unit uPasTri; interface uses // w3system, w3scroll, w3console, w3components, w3application, w3consoleapp, w3lists; System.Types, System.Lists, SmartCL.System, SmartCL.Scroll, SmartCL.Console, SmartCL.Components, SmartCL.Application, SmartCL.ConsoleApp; type TApplication = class(TW3CustomConsoleApplication) protected procedure PopulateConsole; override; end; implementation procedure TApplication.PopulateConsole; var MAX, n, k, Coeff, Divisor : integer; CurrentLine : string; { Factorial by recursion, leaving the factorial in Result. } function Fact(Num : integer) : integer; begin if Num = 0 then Result := 1 else begin Result := Fact(Num - 1); Result := Result * Num; end; end; { Recursive procedure for coefficient suggested by Rolfe } function Coefficient(n, k : integer): integer; begin if (k = 0) or (k = n) then Result := 1 else begin Result := Coefficient(n - 1, k - 1) * n div k; end; end; begin MAX := 20; CurrentLine := ''; Console.writeLn(''); for n := 0 to MAX do begin for k := 0 to n do begin if (k = 0) or (k = n) then Coeff := 1 else Coeff := Coefficient(n, k); CurrentLine := CurrentLine + IntToStr(Coeff) + ' '; Divisor := 100000; while Divisor >= 10 do begin if Coeff div Divisor < 1 then CurrentLine := CurrentLine + ' '; Divisor := Divisor div 10; end; end; Console.writeLn(CurrentLine); CurrentLine := ''; end; Console.writeLn(''); end; end.
Other Examples of Console Applications
We have many examples of console programs generated from Blockly blocks using our BlocklyToSmartPascal online application. This would be a good starting point for those of you without much experience in text-based programming.
See also our demonstrations of a linear search and a binary search.