User Input: Difference between revisions
m (→Input a number: Changed to works with template) |
m (It turns out the whole task needed works with) |
||
Line 4: | Line 4: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|GCC|4.1.2}} |
|||
function Get_String return String is |
function Get_String return String is |
||
Line : String (1 .. 1_000); |
Line : String (1 .. 1_000); |
||
Line 30: | Line 29: | ||
~ |
~ |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QuickBasic|4.5}} |
|||
INPUT "Enter a string: ", s$ |
INPUT "Enter a string: ", s$ |
||
INPUT "Enter a number: ", i% |
INPUT "Enter a number: ", i% |
||
{{works with|FreeBASIC}} |
|||
dim s as string |
dim s as string |
||
dim i as integer |
dim i as integer |
||
Line 52: | Line 51: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{works with|gcc}} |
|||
'''Compiler:''' [[gcc]] |
|||
#include <stdio.h> |
#include <stdio.h> |
||
int main(int argc, char* argv[]) |
int main(int argc, char* argv[]) |
||
Line 66: | Line 65: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{works with|g++}} |
|||
'''Compiler:''' [[g++]] |
|||
#include <iostream> |
#include <iostream> |
||
#include <istream> |
#include <istream> |
||
Line 160: | Line 159: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
'''Interpreter:''' [[Java]] |
|||
import java.io.BufferedReader; |
import java.io.BufferedReader; |
||
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
||
Line 175: | Line 172: | ||
or |
or |
||
{{works with|Java|1.5/5.0+}} |
|||
import java.util.Scanner; |
import java.util.Scanner; |
||
Line 184: | Line 180: | ||
=={{header|mIRC Scripting Language}}== |
=={{header|mIRC Scripting Language}}== |
||
'''Editor:''' [[mIRC]] |
|||
alias askmesomething { |
alias askmesomething { |
||
echo -a You answered: $input(What's your name?, e) |
echo -a You answered: $input(What's your name?, e) |
||
Line 191: | Line 185: | ||
=={{header|newLISP}}== |
=={{header|newLISP}}== |
||
{{works with|newLISP|9.0}} |
|||
(print "Enter an integer: ") |
(print "Enter an integer: ") |
||
(set 'x (read-line)) |
(set 'x (read-line)) |
||
Line 210: | Line 204: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{works with|Perl|5.8.8}} |
|||
#!/usr/bin/perl |
#!/usr/bin/perl |
||
Line 217: | Line 211: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
{{works with|CLI SAPI}} |
|||
#!/usr/bin/php |
#!/usr/bin/php |
||
<?php |
<?php |
||
Line 235: | Line 229: | ||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
{{works with|PostScript|level-2}} |
|||
'''Interpreter:''' Any level-2 PS interpreter |
|||
%open stdin for reading (and name the channel "kbd"): |
%open stdin for reading (and name the channel "kbd"): |
||
/kbd (%stdin) (r) file def |
/kbd (%stdin) (r) file def |
||
Line 266: | Line 259: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{works with|Ruby|1.8.4}} |
|||
print "Enter a string: " |
print "Enter a string: " |
||
s = gets |
s = gets |
||
Line 301: | Line 293: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|Debian Almquish SHell}} |
|||
#!/bin/sh |
#!/bin/sh |
||
Line 308: | Line 299: | ||
read INTEGER |
read INTEGER |
||
{{works with|Bourne Again SHell}} |
|||
#!/bin/bash |
#!/bin/bash |
||
Line 323: | Line 313: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
'''Interpreter:''' Sun [[Java]] |
|||
{{libheader|Swing}} |
{{libheader|Swing}} |
||
Line 337: | Line 325: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|2.5}} |
|||
{{libheader|Tkinter}} |
{{libheader|Tkinter}} |
||
Line 362: | Line 349: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
{{works with|Windows Script Host}} |
|||
strUserIn = InputBox("Enter Data") |
strUserIn = InputBox("Enter Data") |
||
Wscript.Echo strUserIn |
Wscript.Echo strUserIn |
Revision as of 23:38, 7 March 2008
You are encouraged to solve this task according to the task description, using any language you may know.
In this task, the goal is to input a string and the integer 75000, from the normal user interface.
Text Terminal
Ada
function Get_String return String is Line : String (1 .. 1_000); Last : Natural; begin Get_Line (Line, Last); return Line (1 .. Last); end Get_String; function Get_Integer return Integer is S : constant String := Get_String; begin return Integer'Value (S); -- may raise exception Constraint_Error if value entered is not a well-formed integer end Get_Integer; My_String : String := Get_String; My_Integer : Integer := Get_Integer;
ALGOL 68
print("Enter a string: "); STRING s := read string; print("Enter a number: "); INT i := read int; ~
BASIC
INPUT "Enter a string: ", s$ INPUT "Enter a number: ", i%
dim s as string dim i as integer input "Enter a string: ", s input "Enter the integer 75000: ", i
Befunge
This prompts for a string and pushes it to the stack a character at a time (~) until end of input (-1).
<>:v:"Enter a string: " ^,_ >~:1+v ^ _@
Numeric input is easier, using the & command.
<>:v:"Enter a number: " ^,_ & @
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; }
C++
#include <iostream> #include <istream> #include <ostream> #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; }
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
cin >> string_input;
with
readline(cin, string_input);
C#
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.Read(); } } }
Forth
Input a string
: INPUT$ ( n -- addr n ) PAD SWAP ACCEPT PAD SWAP ;
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.
: INPUT# ( -- u true | false ) 0. 16 INPUT$ DUP >R >NUMBER NIP NIP R> <> DUP 0= IF NIP THEN ;
: INPUT# ( -- n true | d 1 | false ) 16 INPUT$ SNUMBER? ;
: INPUT# ( -- n true | false ) 16 INPUT$ NUMBER? NIP DUP 0= IF NIP THEN ;
Note that NUMBER? always leaves a double result on the stack. INPUT# returns a single precision number. If you desire a double precision result, remove the NIP.
Here is an example that puts it all together:
: 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 ;
Groovy
word = System.in.readLine() num = System.in.readLine().toInteger()
Haskell
main = do putStr "Enter a string: " str <- getLine putStr "Enter an integer: " num <- getLine >>= return.read :: IO Int putStrLn $ str ++ (show num)
Note: :: IO Int is only there to disambiguate what type we wanted from read. If num were used in a numerical context, its type would have been inferred by the interpreter/compiler.
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(); } }
or
import java.util.Scanner; Scanner stdin = new Scanner(System.in); String string = stdin.nextLine(); int number = stdin.nextInt();
mIRC Scripting Language
alias askmesomething { echo -a You answered: $input(What's your name?, e) }
newLISP
(print "Enter an integer: ") (set 'x (read-line)) (print "Enter a string: ") (set 'y (read-line))
Pascal
program UserInput(input, output); var i : Integer; s : String; begin write('Enter an integer: '); readln(i); write('Enter a string: '); readln(s) end.
Perl
#!/usr/bin/perl my $string = <>; # equivalent to readline(*STDIN) my $integer = <>;
PHP
#!/usr/bin/php <?php $string = fgets(STDIN); $integer = (int) fgets(STDIN);
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();
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
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:
%if the read was successful, convert the string to integer: {cvi} if
which will read the conversion operator 'cvi' (convert to integer) and the boolean and execute the former if the latter is true.
PowerShell
$string = Read-Host "Input a string" $number = Read-Host "Input a number"
Python
string = raw_input("Input a string: ") number = input("Input a number: ") #Will raise an error if a malformed integer is entered, #but will accept just about anything.
Raven
'Input a string: ' print expect as str 'Input an integer: ' print expect 0 prefer as num
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
Scheme
The read procedure is R5RS standard, inputs a scheme representation so, in order to read a string, one must enter "hello world"
(define str (read)) (define num (read)) (display "String = ") (display str) (display "Integer = ") (display num)
Tcl
Like LISP, there is no concept of a "number" in TCL - the only real variable type is a string (whether a string might represent a number is a matter of interpretation of the string in a mathematical expression at some later time). Thus the input is the same for both tasks:
set str [gets stdin] set num [gets stdin]
possibly followed by something like
if {![string is integer $num]} then { ...do something here...}
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
UNIX Shell
#!/bin/sh read STRING read INTEGER
#!/bin/bash read STRING read INTEGER
GUI
AppleScript
set input to text returned of (display dialog "Enter text:" default answer "")
set input to text returned of (display dialog "Enter a number:" default answer "") as integer
Java
import javax.swing.*; public class GetInputSwing { public static void main(String[] args) throws Exception { int number = Integer.parseInt(JOptionPane.showInputDialog ("Enter an Integer")); String string = JOptionPane.showInputDialog ("Enter a String"); } }
Python
import tkSimpleDialog number = tkSimpleDialog.askinteger("Integer", "Enter a Number") string = tkSimpleDialog.askstring("String", "Enter a String")
Tcl
# create entry widget: pack [entry .e1] # read its content: set input [.e get]
Alternatively, the content of the widget can be tied to a variable:
pack [entry .e1 -textvar input] # show the content at any time by puts $input
The -validate option can be used to test the contents/edits of the widget at any time against any parameters (including testing string is integer when the user hits <Return> or such)
VBScript
strUserIn = InputBox("Enter Data") Wscript.Echo strUserIn