Code Snippet: User Input-Multi Line

From Coder Merlin™

Reading multiple lines of text from the console

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


// Read multiple lines from the console until end-of-file
var line : String?
repeat {
    line = readLine()                // Read a single line of input from the console
    if let text = line {             // Let's check if it's not nil, if so, it's really a string
        print("You typed: \(text)")  // Print the string
} while line != nil                  // Continue until end-of-file


text = ""
while text is not None:
        text = input()
        text = None
    print("You typed: %s" %text)


#include <stdio.h>
#include <string.h>

int main(void){

  char line_buffer[64]; // Declare character array, capable of holding 63 characters
  do {
    memset(line_buffer,0,64); // Reset value of line_buffer

    fgets(line_buffer,64,stdin); // Save input from stdin to line_buffer 

    printf("You typed: %s\n",line_buffer);
  } while(strlen(line_buffer) != 0); // Verify that user didn't provide EOF
  return 0; // Indicate successful run to OS