User input/Text: Difference between revisions

m
Fixed lang tags.
m (→‎{{header|JavaScript}}: spidermonkey's javascript implementation has print/readline)
m (Fixed lang tags.)
Line 4:
=={{header|Ada}}==
{{works with|GCC|4.1.2}}
<lang ada>function Get_String return String is
Line : String (1 .. 1_000);
function Get_String return String is
Last : Natural;
Line : String (1 .. 1_000);
begin
Last : Natural;
Get_Line (Line, Last);
begin
return Line (1 Get_Line (Line,.. Last);
end Get_String;
return Line (1 .. Last);
function Get_StringGet_Integer return StringInteger is
end Get_String;
S : constant String := Get_String;
function Get_Integer return Integer is
begin
S : constant String := Get_String;
return Integer'Value (S);
begin
-- may raise exception Constraint_Error if value entered is not a well-formed integer
return Integer'Value (S);
end Get_Integer;</lang>
-- may raise exception Constraint_Error if value entered is not a well-formed integer
end Get_Integer;
</lang>
The functions above may be called as shown below
<lang ada>My_String : String := Get_String;
My_Integer : Integer := Get_Integer;</lang>
My_String : String := Get_String;
My_Integer : Integer := Get_Integer;
</lang>
 
=={{header|ALGOL 68}}==
<lang algol68>print("Enter a string: ");
STRING s := read string;
print("Enter a number: ");
INT i := read int;
~</lang>
~
=={{header|AutoHotkey}}==
===Windows console===
<lang AutoHotkey>DllCall("AllocConsole")
DllCall("AllocConsole")
FileAppend, please type something`n, CONOUT$
FileReadLine, line, CONIN$, 1
Line 70 ⟶ 65:
=={{header|AWK}}==
This demo shows a same-line prompt, and that the integer i becomes 0 if the line did not parse as an integer.
<lang awk>~/src/opt/run $ awk 'BEGIN{printf "enter a string: "}{s=$0;i=$0+0;print "ok,"s"/"i}'
<lang awk>
~/src/opt/run $ awk 'BEGIN{printf "enter a string: "}{s=$0;i=$0+0;print "ok,"s"/"i}'
enter a string: hello world
ok,hello world/0
75000
ok,75000/75000</lang>
</lang>
 
=={{header|BASIC}}==
Line 92 ⟶ 85:
=={{header|Befunge}}==
This prompts for a string and pushes it to the stack a character at a time ('''~''') until end of input (-1).
<lang befunge><>:v:"Enter a string: "
^,_ >~:1+v
^ _@</lang>
 
Numeric input is easier, using the '''&''' command.
<lang befunge><>:v:"Enter a number: "
^,_ & @</lang>
 
=={{header|C}}==
{{works with|gcc}}
<lang c> #include <stdio.h>
int main(int argc, char* argv[])
{
int input;
if((scanf("%d", &input))==1)
{
printf("Read in %d\n", input);
return 1;
}
return 0;
}</lang>
 
=={{header|C++}}==
{{works with|g++}}
<lang cpp> #include <iostream>
#include <string>
using namespace std;
 
int main()
{
// while probably all current implementations have int wide enough for 75000, the C++ standard
// only guarantees this for long int.
long int integer_input;
string string_input;
cout << "Enter an integer: ";
cin >> integer_input;
cout << "Enter a string: ";
cin >> string_input;
return 0;
}</lang>
 
Note: The program as written above only reads the string up to the first whitespace character. To get a complete line into the string, replace
Line 143 ⟶ 136:
 
=={{header|C sharp|C#}}==
<lang csharp> using System;
 
namespace C_Sharp_Console {
 
class example {
 
static void Main() {
string word;
int num;
Console.Write("Enter an integer: ");
num = Console.Read();
Console.Write("Enter a String: ");
word = Console.ReadLine();
}
}
}</lang>
 
=={{header|Clojure}}==
<lang clojurelisp>(import '(java.util Scanner))
(def scan (Scanner. *in*))
(def s (.nextLine scan))
(def n (.nextInt scan))</lang>
</lang>
 
=={{header|Common Lisp}}==
Line 194 ⟶ 186:
 
=={{header|Erlang}}==
<lang erlang> {ok, [String]} = io:fread("Enter a string: ","~s").
{ok, [Number]} = io:fread("Enter a number: ","~d").</lang>
 
Alternatively, you could use io:get_line to get a string:
Line 202 ⟶ 194:
=={{header|FALSE}}==
FALSE has neither a string type nor numeric input. Shown instead are routines to parse and echo a word and to parse and interpret a number using the character input command (^).
<lang false>[[^$' =~][,]#,]w:
[0[^'0-$$9>0@>|~][\10*+]#%]d:
w;! d;!.</lang>
 
=={{header|Forth}}==
===Input a string===
 
<lang forth> : INPUT$ ( n -- addr n )
PAD SWAP ACCEPT
PAD SWAP ;</lang>
 
===Input a number===
The only ANS standard number interpretation word is >NUMBER ( ud str len -- ud str len ), which is meant to be the base factor for more convenient (but non-standard) parsing words.
<lang forth> : INPUT# ( -- u true | false )
0. 16 INPUT$ DUP >R
>NUMBER NIP NIP
R> <> DUP 0= IF NIP THEN ;</lang>
 
{{works with|GNU Forth}}
<lang forth> : INPUT# ( -- n true | d 1 | false )
16 INPUT$ SNUMBER? ;</lang>
 
{{works with|Win32Forth}}
<lang forth> : INPUT# ( -- n true | false )
16 INPUT$ NUMBER? NIP
DUP 0= IF NIP THEN ;</lang>
 
Note that NUMBER? always leaves a double result on the stack.
Line 234 ⟶ 226:
Here is an example that puts it all together:
 
<lang forth> : TEST
." Enter your name: " 80 INPUT$ CR
." Hello there, " TYPE CR
." Enter a number: " INPUT# CR
IF ." Your number is " .
ELSE ." That's not a number!" THEN CR ;</lang>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<lang fortran>character(20) :: s
character(20) :: s
integer :: i
 
Line 250 ⟶ 241:
read*, s
print*, "Enter the integer 75000"
read*, i</lang>
</lang>
 
=={{header|Groovy}}==
<lang groovy> word = System.in.readLine()
num = System.in.readLine().toInteger()</lang>
 
=={{header|Haskell}}==
<lang haskell>import System.IO (hFlush, stdout)
main = do
import System.IO (hFlush, stdout)
putStr "Enter a string: "
main = do
import System.IO ( hFlush, stdout)
putStr "Enter a string: "
str hFlush<- stdoutgetLine
putStr str"Enter <-an getLineinteger: "
hFlush stdout
putStr "Enter an integer: "
num <- readLn :: IO Int
hFlush stdout
putStrLn $ str ++ (show num)</lang>
num <- readLn :: IO Int
putStrLn $ str ++ (show num)</lang>
Note: <tt>:: IO Int</tt> is only there to disambiguate what type we wanted from <tt>read</tt>. If <tt>num</tt> were used in a numerical context, its type would have been inferred by the interpreter/compiler.
Note also: Haskell doesn't automatically flush stdout when doing input, so explicit flushes are necessary.
 
=={{header|J}}==
<lang j>require 'misc' NB. load system script</lang>
<lang j>
<lang j>prompt 'Enter string: '
require 'misc' NB. load system script
</lang>
<lang j>
prompt 'Enter string: '
 
0".prompt 'Enter 75000: '</lang>
</lang>
Both of these sentences return the user provided input. But this implementation implements no error checking: For example, if a different number is entered at the second prompt, it will be used instead of 75000.
 
=={{header|Java}}==
<lang java> import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class GetInput {
public static void main(String[] args) throws Exception {
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
int number = Integer.parseInt(sysin.readLine());
String string = sysin.readLine();
}
}</lang>
 
or
 
{{works with|Java|1.5/5.0+}}
<lang java> import java.util.Scanner;
 
Scanner stdin = new Scanner(System.in);
String string = stdin.nextLine();
int number = stdin.nextInt();</lang>
 
=={{header|JavaScript}}==
Line 326 ⟶ 311:
=={{header|Logo}}==
Logo literals may be read from a line of input from stdin as either a list or a single word.
<lang logo> make "input readlist ; in: string 75000
show map "number? :input ; [false true]
 
make "input readword ; in: 75000
show :input + 123 ; 75123
make "input readword ; in: string 75000
show :input ; string 75000</lang>
 
=={{header|Mathematica}}==
<lang Mathematica>mystring = InputString["give me a string please"];
mystringmyinteger = InputStringInput["give me aan stringinteger please"];</lang>
myinteger = Input["give me an integer please"];
</lang>
 
=={{header|Metafont}}==
Line 358 ⟶ 341:
 
=={{header|mIRC Scripting Language}}==
<lang mirc>alias askmesomething {
echo -a You answered: $input(What's your name?, e)
}</lang>
}
 
=={{header|Modula-3}}==
Line 376 ⟶ 359:
number := IO.GetInt();
IO.Put("You entered: " & string & " and " & Fmt.Int(number) & "\n");
END Input.</lang>
</lang>
 
=={{header|newLISP}}==
{{works with|newLISP|9.0}}
<lang lisp>(print "Enter an integer: ")
(set 'x (read-line))
(print "Enter a string: ")
(set 'y (read-line))</lang>
 
=={{header|OCaml}}==
Line 414 ⟶ 396:
=={{header|Pascal}}==
 
<lang pascal> program UserInput(input, output);
var i : Integer;
s : String;
begin
write('Enter an integer: ');
readln(i);
write('Enter a string: ');
readln(s)
end.</lang>
 
=={{header|Perl}}==
{{works with|Perl|5.8.8}}
<lang perl> #!/usr/bin/perl
 
my $string = <>; # equivalent to readline(*STDIN)
my $integer = <>;</lang>
 
=={{header|PHP}}==
{{works with|CLI SAPI}}
<lang php> #!/usr/bin/php
<?php
$string = fgets(STDIN);
$integer = (int) fgets(STDIN);</lang>
 
=={{header|Pop11}}==
<lang pop11> ;;; Setup item reader
lvars itemrep = incharitem(charin);
lvars s, c, j = 0;
;;; read chars up to a newline and put them on the stack
while (charin() ->> c) /= `\n` do j + 1 -> j ; c endwhile;
;;; build the string
consstring(j) -> s;
;;; read the integer
lvars i = itemrep();</lang>
 
=={{header|PostScript}}==
{{works with|PostScript|level-2}}
<lang postscript> %open stdin for reading (and name the channel "kbd"):
/kbd (%stdin) (r) file def
%make ten-char buffer to read string into:
/buf (..........) def
%read string into buffer:
kbd buf readline</lang>
 
At this point there will be two items on the stack: a boolean which is "true" if the read was successful and the string that was read from the kbd (input terminates on a <return>). If the length of the string exceeds the buffer length, an error condition occurs (rangecheck). For the second part, the above could be followed by this:
 
<lang postscript> %if the read was successful, convert the string to integer:
{cvi} if</lang>
 
which will read the conversion operator 'cvi' (convert to integer) and the boolean and execute the former if the latter is true.
Line 467 ⟶ 449:
=={{header|PowerShell}}==
 
<lang powershell> $string = Read-Host "Input a string"
$number = Read-Host "Input a number"</lang>
 
=={{header|Python}}==
Line 489 ⟶ 471:
{{works with|R|2.81}}
 
<lang R>stringval <- readline("String: ")
<lang R>
stringvalintval <- as.integer(readline("StringInteger: "))</lang>
intval <- as.integer(readline("Integer: "))
</lang>
 
=={{header|Raven}}==
<lang raven> 'Input a string: ' print expect as str
'Input an integer: ' print expect 0 prefer as num</lang>
 
=={{header|REXX}}==
Line 506 ⟶ 486:
=={{header|Ruby}}==
{{works with|Ruby|1.8.4}}
<lang ruby> print "Enter a string: "
s = gets
print "Enter an integer: "
i = gets.to_i # If string entered, will return zero
puts "String = " + s
puts "Integer = " + i.to_s</lang>
 
=={{header|Scheme}}==
The <tt>read</tt> procedure is R5RS standard, inputs a scheme representation so, in order to read a string, one must enter <tt>"hello world"</tt>
<lang scheme> (define str (read))
(define num (read))
(display "String = ") (display str)
(display "Integer = ") (display num)</lang>
 
=={{header|Slate}}==
<lang slate>print: (query: 'Enter a String: ').
print: (query: 'Enter a String: ').
[| n |
n: (Integer readFrom: (query: 'Enter an Integer: ')).
Line 528 ⟶ 507:
ifTrue: [print: n]
ifFalse: [inform: 'Not an integer: ' ; n printString]
] do.</lang>
</lang>
 
=={{header|Smalltalk}}==
Line 580 ⟶ 558:
This program leaves the requested values in the global variables ''s'' and ''integer''.
 
<lang awkti89b>Prgm
<pre style="font-family:'TI Uni'">Prgm
InputStr "Enter a string", s
Loop
Line 590 ⟶ 568:
EndIf
EndLoop
EndPrgm</prelang>
 
=={{header|Toka}}==
<lang toka>needs readline
." Enter a string: " readline is-data the-string
." Enter a number: " readline >number [ ." Not a number!" drop 0 ] ifFalse is-data the-number
 
the-string type cr
the-number . cr</lang>
 
=={{header|UNIX Shell}}==
{{works with|Debian Almquish SHell}}
<lang bash>#!/bin/sh
 
read STRING
read INTEGER</lang>
{{works with|Bourne Again SHell}}
<lang bash>#!/bin/bash
 
read STRING
read INTEGER</lang>
 
=={{header|Vedit macro language}}==
<lang vedit>Get_Input(1, "Enter a string: ")
#2 = Get_Num("Enter a number: ")</lang>
 
=={{header|Visual Basic .NET}}==
Anonymous user