Difference between revisions of "Read text from standard input"

From CodeCodex

(added haskell)
(c++ example added)
Line 1: Line 1:
 
==Implementations==
 
==Implementations==
 +
===C++===
 +
<HIGHLIGHTSYNTAX language="c++">#include <iostream>
 +
#include <string>
 +
getline(cin, line);
 +
cout<<"You entered: "<<line<<endl;
 +
</HIGHLIGHTSYNTAX>
 +
 
===Common Lisp===
 
===Common Lisp===
 
<pre>
 
<pre>
Line 44: Line 51:
 
</HIGHLIGHTSYNTAX>
 
</HIGHLIGHTSYNTAX>
  
 +
[[Category:C++]]
 
[[Category:Common Lisp]]
 
[[Category:Common Lisp]]
 
[[Category:Haskell]]
 
[[Category:Haskell]]

Revision as of 10:57, 26 November 2009

Implementations

C++

<HIGHLIGHTSYNTAX language="c++">#include <iostream>

  1. include <string>

getline(cin, line); cout<<"You entered: "<<line<<endl; </HIGHLIGHTSYNTAX>

Common Lisp

(progn (write-string "> prompt ") (read-line))
> prompt fooey
"fooey" ;
NIL

Haskell

Prelude> do putStr "> prompt "; getLine
> prompt fooey
"fooey"

Java

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while (str != null) {
            System.out.print("> prompt ");
            str = in.readLine();
            process(str);
        }
    } catch (IOException e) {
    }

OCaml

# Printf.printf "> prompt %!";
  input_line stdin;;
> prompt fooey
- : string = "fooey"

Python

<HIGHLIGHTSYNTAX language="python"> s = raw_input('prompt> ') print s </HIGHLIGHTSYNTAX>