Difference between revisions of "Code Snippet: User Input-Single Line"

From Coder Merlin
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Reading a single line of text from the console =
= Reading a single line of text from the console =
Note: Input read from the console is terminated with a ^D (Control-D) which indicates the end-of-file.
:Note: Input read from the console is terminated with a ^D (Control-D) which indicates the end-of-file.


== Swift ==
== Swift ==
Line 26: Line 26:
     text = ""
     text = ""
print("You typed: %s" %text)
print("You typed: %s" %text)
</syntaxhighlight>
== Perl ==
<syntaxhighlight lang="perl">
my $input = <STDIN>;
print "You typed: $input";
</syntaxhighlight>
== C ==
<syntaxhighlight lang="c">
#include <stdio.h>
int main(void){
  char line_buffer[64]; // Declare character array capable of holding 63 characters
  // Read input from stdin via fgets
  // check fgets return for NULL if EOF is passed
  if(fgets(line_buffer,64,stdin) == NULL){
    return 0; // Indicate successful run to the OS
  } 
  printf("You typed: %s\n",line_buffer); //Output input
  return 0; // Indicate a successful run to the OS
}
</syntaxhighlight>
== C++ ==
<syntaxhighlight lang="c++">
#include <iostream>
#include <string>
int main() {
    std::string input;
    std::getline(std::cin, input);
 
    if (!std::cin.fail()) {
      std::cout << "You typed: " << input;
    }
 
    return 0;
}
</syntaxhighlight>
== Common Lisp ==
<syntaxhighlight lang="lisp">
(defvar input)
(setq input (read-line))
(format t "You typed: ~(~s~) ~%"
              input)
</syntaxhighlight>
== Ruby ==
<syntaxhighlight lang="ruby">
text = gets
puts "You typed: " + text
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 10:06, 24 August 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Reading a single line of text from the console[edit]

Note: Input read from the console is terminated with a ^D (Control-D) which indicates the end-of-file.

Swift[edit]

// Read a single line of input from the console
let line = readLine()            // This will return an optional string
if line != nil {                 // Let's check if it's not nil, if so, it's really a string
    let text = line!             // Unwrap the string
    print("You typed: \(text)")  // Print the string
}
// Read a single line of input from the console (syntactic sugar)
if let text = readLine() {
    print("You typed: \(text)")
}

Python[edit]

try:
    text = input()
except:
    text = ""
print("You typed: %s" %text)

Perl[edit]

my $input = <STDIN>;

print "You typed: $input";

C[edit]

#include <stdio.h>

int main(void){

  char line_buffer[64]; // Declare character array capable of holding 63 characters

  // Read input from stdin via fgets
  // check fgets return for NULL if EOF is passed
  if(fgets(line_buffer,64,stdin) == NULL){
    return 0; // Indicate successful run to the OS
  }  
  printf("You typed: %s\n",line_buffer); //Output input

  return 0; // Indicate a successful run to the OS
}

C++[edit]

#include <iostream>
#include <string>

int main() {
    std::string input;
    std::getline(std::cin, input);
  
    if (!std::cin.fail()) {
      std::cout << "You typed: " << input;
    }
  
    return 0;
}

Common Lisp[edit]

(defvar input)

(setq input (read-line))

(format t "You typed: ~(~s~) ~%"
              input)

Ruby[edit]

text = gets
puts "You typed: " + text