31
\$\begingroup\$

At runtime, keep prompting for a line of input until the user inputs something (other than an empty newline), i.e. does not just press Enter or OK. Output or result is neither required nor prohibited.

Pseudo-code 1

myform = new form("GUI")
myform.mytxt = new editfield("")
myform.ok = new button("OK")
repeat
  waitfor(myform.ok,"click")
until myform.mytxt.content <> ""

Pseudo-code 2

LET TEXT = ""
WHILE TEXT = "" DO
  TEXT = PROMPT("")
ENDWHILE

Example 1

Program runs and immediately pops up a form with a single text field and an OK button.
User clicks the OK button.
Nothing happens.
User pastes "hello world" into the text field and clicks the OK button.
Program terminates.

Example 2

Function is called and immediately displays a blank line and a blinking cursor.
User presses Enter.
Cursor moves down one line.
User presses Enter.
Cursor moves down one line.
User presses PPCGEnter
Function returns.

\$\endgroup\$
  • \$\begingroup\$ is it allowed to count space-only lines as empty? \$\endgroup\$ – 12431234123412341234123 May 4 '17 at 9:25
  • \$\begingroup\$ @12431234123412341234123 No. \$\endgroup\$ – Adám May 4 '17 at 9:29
  • \$\begingroup\$ Is it necessary to wait for the full line once the user starts to input data? \$\endgroup\$ – Matteo Italia May 7 '17 at 13:22
  • \$\begingroup\$ Ok, from a comment in @CodyGray's deleted answer it seems like it's not allowed. \$\endgroup\$ – Matteo Italia May 7 '17 at 14:54
  • \$\begingroup\$ @MatteoItalia Yes, correct; a line of input. \$\endgroup\$ – Adám May 7 '17 at 15:17

61 Answers 61

2
\$\begingroup\$

Perl, 15 12 bytes (11 + -n flag)

-3 bytes thanks to manatwork

1/0if$/ne$_

Explanation:

The -n flag places a read loop around the program, turning it into:

LINE:
while (<>) {
    1/0 if $/ ne $_  # Spaces added for clarity.
}

Where "<>" reads a line from stdin to the variable $_. If the line contained more than the ending newline (ne is string inequality, $/ is the input record separator which defaults to newline), a division by zero is performed which causes the program to output an error message and terminate.

\$\endgroup\$
  • 3
    \$\begingroup\$ Welcome to PPCG. Can you explain your code? \$\endgroup\$ – Adám May 4 '17 at 7:05
  • \$\begingroup\$ As 3aw5TZetdf suggests in his tip in Tips for golfing in Perl?, “Use $/ instead of "\n"”. And if you reverse the compered terms, you will not need the space. \$\endgroup\$ – manatwork May 4 '17 at 7:44
  • 4
    \$\begingroup\$ @user68838 Are you the OP? Did you create a new account to edit this? \$\endgroup\$ – Adám May 4 '17 at 8:06
2
\$\begingroup\$

Clojure, 32 bytes

(some seq(repeatedly read-line))

some returns the first true value of (seq x).

seq gives a sequence for a collection (e.g. a String) or nil if the collection is empty.

nil counts as false for some, so it will ignore any empty input, completing when a non-empty string is entered.

\$\endgroup\$
2
\$\begingroup\$

MATL, 4 bytes

`jn~

This needs to be executed with the MATL interpreter running in Octave (not in Matlab). The reason is that in Matlab, unlike in Octave, the underlying function input('','s') (unevaluated input) interprets an input consisting of only spaces as empty input.

Gif or it didn't happen:

enter image description here

Explanation

`     % Do...while loop
  j   %   Take input as a string
  n   %   Number of elements
  ~   %   Negate. Gives true if number of elements was zero, and false otherwise
      % End loop (implicit). If top of the stack is true: next iteration. Else: Exit
\$\endgroup\$
  • 1
    \$\begingroup\$ "Gif or it didn't happen" That's the best part! \$\endgroup\$ – Arjun May 5 '17 at 3:51
2
\$\begingroup\$

Ruby, 8 bytes (7 + '-n' flag)

~/./&&a

Try it online!

\$\endgroup\$
2
\$\begingroup\$

ZX81 BASIC, 20 bytes

10 INPUT A$
20 IF NOT LEN A$ THEN RUN

Because ZX81 BASIC is tokenised, NOT LEN A$ is a byte shorter then A$<>"". Program lines count 5 bytes each including the line number.

\$\endgroup\$
  • \$\begingroup\$ Why can't you use line numbers 1 and 2? \$\endgroup\$ – Adám May 5 '17 at 13:03
  • \$\begingroup\$ @Adám Line numbers are stored internally as 16-bit integers, so they don't affect the byte count. \$\endgroup\$ – Neil May 5 '17 at 14:33
2
\$\begingroup\$

Bash + line, 16 bytes

[ "`line`" ]||$0

Backticks are short for $(), to execute a command. line reads a line. If line is empty, then [ will return false, and thus $0 will be executed - the 0th argument which refers to itself. If there is any non-zero string, [ will return true and because of lazy evaluation $0 won't be executed (|| is a logical OR).

Notes

Instead of line you can also use head -n1, which is more common (but also longer).

In all cases I tested for empty lines, which behaves accordingly. And the following special inputs that may break the behavior (but did not ;) ):

  • -x
  • aoeu aoeu aoeu
  • !

The only thing that breaks it is escaping the newline with \, or using a character. But how to handle these special characters is not mentioned in the original question.

\$\endgroup\$
  • \$\begingroup\$ Hi, and welcome to PPCG! This seems like an entirely reasonable answer. Sometimes I like to use multiple headers for posts which have multiple answers like this, e.g. "# bash + line, 16 bytes" … "# pure Bash, 19 bytes", but that's optional. Note that if you have dependencies on an external program or library (like line in this case), it helps to mention that in the header. (Also, I think the last solution might break if the user types something like -x? Not sure on that one.) \$\endgroup\$ – user62131 May 5 '17 at 11:15
  • \$\begingroup\$ Thank you, I updated the answer accordingly. The last solution, somehow it didn't work at all. But now I updated it so that it works. I tested all of them and listed which input I tested for. There exist no special parameters that would break anything to my knowledge. \$\endgroup\$ – Yeti May 5 '17 at 16:12
  • \$\begingroup\$ I'd rather you post each answer separately so people (including me) can vote for specific answers. \$\endgroup\$ – Adám May 5 '17 at 17:30
  • \$\begingroup\$ They're now separated, however I'm sure you'll upvote all of them anyway :) \$\endgroup\$ – Yeti May 5 '17 at 18:05
2
\$\begingroup\$

Pure Bash, 19 bytes

A pure Bash solution using recursion in which a line is read into the variable $a (19 bytes):

read a;[ "$a" ]||$0

Notes

In all cases I tested for empty lines, which behaves accordingly. And the following special inputs that may break the behavior (but did not ;) ):

  • -x
  • aoeu aoeu aoeu
  • !

The only thing that breaks it is escaping the newline with \, or using a character. But how to handle these special characters is not mentioned in the original question.

\$\endgroup\$
  • \$\begingroup\$ Do you need the spaces between the brackets? \$\endgroup\$ – OldBunny2800 May 7 '17 at 17:10
2
\$\begingroup\$

x86 Assembler (NASM - Linux), 227 bytes

SECTION .bss
i: resb 5

SECTION .text
global  _start

_start:
    mov     edx, 5
    mov     ecx, i
    mov     ebx, 0
    mov     eax, 3
    int     80h
    cmp byte [ecx], 0xa
    je _start
    mov     eax, 1
    int     80h
\$\endgroup\$
  • \$\begingroup\$ Well done! Nice first answer \$\endgroup\$ – Christopher May 6 '17 at 15:43
  • 1
    \$\begingroup\$ Umm, is this really 227 bytes? Usually we count assembler variants based on the size of the compiled outcome. \$\endgroup\$ – Erik the Outgolfer May 7 '17 at 14:13
2
\$\begingroup\$

Red, 20 16 Bytes

until[""<>ask""]

Explanation:

until   does the code that's inside the [blocks] until it's true
<>      checks empty string "" isn't returned by rest of code
ask ""  prompts with "" (empty msg), returns user input

Thanks to @endo64 for the code

-4 bytes thanks to @sqlab

\$\endgroup\$
  • 1
    \$\begingroup\$ Save 4 bytes by omitting the spaces \$\endgroup\$ – sqlab May 5 '17 at 7:59
2
\$\begingroup\$

x86 machine code on DOS, 13 bytes

00000000  43 b4 01 cd 21 3c 0d 75  f7 4b 74 f4 c3           |C...!<.u.Kt..|
0000000d

Commented assembly:

    org 100h

section .text

    ; notice: on virtually every DOS bx starts at 0 at program start
    ; see http://www.fysnet.net/yourhelp.htm
start:
    inc bx      ; bx counts the number of read characters; increment it
    mov ah,1
    int 21h     ; read a character into al
    cmp al,0dh  ; check if it's a newline
    jne start   ; repeat if it's not
    dec bx      ; got a newline; decrement the counter
    jz start    ; if the counter became 0, it means that we only read the
                ; newline; restart (with bx being 0, as on startup)
    ret         ; otherwise quit
\$\endgroup\$
1
\$\begingroup\$

QBasic, 34 bytes

INPUT A$
WHILE A$=""
INPUT A$
WEND

I think nothing can be more self-explanatory than that. To test, go to this website and copy-paste the following code in their text-editor :

10 INPUT A$
20 WHILE A$=""
30 INPUT A$
40 WEND

The reason why line numbers are required is that their website only supports unstructured BASIC.

\$\endgroup\$
  • \$\begingroup\$ Why the final END? \$\endgroup\$ – Adám May 4 '17 at 4:59
  • \$\begingroup\$ @Adám The final END is required to terminate the program when the interpreter comes out of the while loop (WEND is to terminate While Loop) \$\endgroup\$ – Arjun May 4 '17 at 5:13
  • \$\begingroup\$ So what happens if there is no more program to execute, even though END hasn't been reached? \$\endgroup\$ – Adám May 4 '17 at 5:14
  • \$\begingroup\$ @Adám According to the standard, a program must have an END statement as the last statement. However, many interpreters don't require it. But I will keep it just for the sake of compatibility. \$\endgroup\$ – Arjun May 4 '17 at 5:22
  • 1
    \$\begingroup\$ This has been discussed on Meta, concluding that a language is defined by its implementation, not its documentation. \$\endgroup\$ – Adám May 4 '17 at 5:26
1
\$\begingroup\$

C#, 36 bytes

_=>while(System.Console.Read()==13);

Compiles to a Action<int>.

Or if a full program is required, 62 bytes.

class P{static void Main(){while(System.Console.Read()==13);}}
\$\endgroup\$
1
\$\begingroup\$

Batch, 35 bytes

@set s=
@set/ps=
@if "%s%"=="" %0

Rather unfortunately, Batch doesn't clear the variable if you don't enter anything.

\$\endgroup\$
  • \$\begingroup\$ I guess this crashes if %s% == " \$\endgroup\$ – stevefestl May 7 '17 at 7:08
  • 1
    \$\begingroup\$ @SteveFest Indeed, but it still stops insisting on an answer... you can probably crash-resist it by using %s:"=""%. \$\endgroup\$ – Neil May 7 '17 at 9:12
  • \$\begingroup\$ It would make a error message without the replace... Which is unnecessary \$\endgroup\$ – stevefestl May 7 '17 at 9:13
1
\$\begingroup\$

Lithp, 90 bytes

((import readline)(readline "?" #A::((f A)))(def f #A::(if(== "" A)((readline "?"(get f/1)

Try it online! (Also works on command line in node)

A side effect of the current parser is that not all trailing ) brackets are required. The very last one is, but the rest can be omitted saving quite a few bytes.

The readline module works in browser and via command line, and uses a callback. We exploit this to call readline before defining f, and save many bytes not needed all the closing brackets to invoke f after it's defined.

\$\endgroup\$
1
\$\begingroup\$

Kotlin, 38 35 29 bytes

-3 bytes by negating any() rather than calling isEmpty()
-6 bytes thanks to Adám pointing out the function doesn't need to be named to be called

{while(!readLine()!!.any());}

called like so:

{while(!readLine()!!.any());}()

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Something wrong with your TIO link. \$\endgroup\$ – Adám May 4 '17 at 11:57
  • \$\begingroup\$ You just have to click the click here link to remove the message about TIO v2 \$\endgroup\$ – The_Lone_Devil May 4 '17 at 11:58
  • \$\begingroup\$ In that case you could just link to tio.run/nexus, but why not fill in the necessary fields on tio.run/nexus/kotlin and get the full URL (hint: press Esc, S)? \$\endgroup\$ – Adám May 4 '17 at 12:00
  • \$\begingroup\$ @Adám Updated the link to use tio.run/nexus, never used TIO before :) \$\endgroup\$ – The_Lone_Devil May 4 '17 at 12:02
  • \$\begingroup\$ Nice. I think you only have to count the 29 char brace, as the function does not need to be named in order to be called. \$\endgroup\$ – Adám May 4 '17 at 12:06
1
\$\begingroup\$

AHK, 19 bytes

While !a
InputBox,a

Not very interesting, I'm afraid.

\$\endgroup\$
1
\$\begingroup\$

Ruby, 19 bytes

while gets==?\n;end
\$\endgroup\$
1
\$\begingroup\$

Scala, 21 bytes

while(readLine==""){}

Scala can run code as a script without the need for a class/main method so this is a complete program.

readLine is deprecated since Scala 2.11 but it's still available in 2.12

\$\endgroup\$
1
\$\begingroup\$

QBIC, 7 5 bytes

≈A|_?

Saved 2 bytes because empty string is falsy.

Explanation:

≈ |     WHILE <condition>
 A      A$ gets set by _? below. Empty string is false, so it stays in the WHILE
   _?   Ask the user for input, implicitly assign to A$
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 20 bytes

While[Input[]==Null]
\$\endgroup\$
1
\$\begingroup\$

REXX, 24 bytes

do until n>''
  pull n
end
\$\endgroup\$
1
\$\begingroup\$

Tcl, 26

while {![gets stdin t]} {}

demo

\$\endgroup\$
1
\$\begingroup\$

Excel VBA 25 bytes

while Inputbox(0)="":wend

You can run it via immediate window. It's a simple while loop with inputbox.

\$\endgroup\$
1
\$\begingroup\$

Pure Bash, 21 bytes

A pure Bash solution using shift and recursion with parameter (21 bytes):

shift||(read a;$0 $a)

shift shifts the arguments (and defaults to shifting 1). Which fails if there is no argument, upon which it will try to execute itself again with the next read line as the first argument.

Note that double quotes are here not needed if the line contains spaces, because it does not matter if it's just one word or multiple.

Notes

In all cases I tested for empty lines, which behaves accordingly. And the following special inputs that may break the behavior (but did not ;) ):

  • -x
  • aoeu aoeu aoeu
  • !

The only thing that breaks it is escaping the newline with \, or using a character. But how to handle these special characters is not mentioned in the original question.

\$\endgroup\$
1
\$\begingroup\$

Bash + line, 16 bytes

Another approach using recursion with parameter (16 bytes), using shift:

shift||$0 `line`

shift shifts the arguments (and defaults to shifting 1). Which fails if there is no argument, upon which it will try to execute itself again with the next read line as the first argument.

Notes

Instead of line you can also use head -n1, which is more common (but also longer).

In all cases I tested for empty lines, which behaves accordingly. And the following special inputs that may break the behavior (but did not ;) ):

  • -x
  • aoeu aoeu aoeu
  • !

The only thing that breaks it is escaping the newline with \, or using a character. But how to handle these special characters is not mentioned in the original question.

\$\endgroup\$
1
\$\begingroup\$

Pyke, 2 bytes

W!

Try it online!

See the debug output to show it's actually reading input

W  - do v
 ! -  not(input())
   - while ^
\$\endgroup\$
1
\$\begingroup\$

Javascript, 17 bytes

for(;!prompt(););

A for alternative to the while answer already here

\$\endgroup\$
1
\$\begingroup\$

C, 83 bytes

e(char*t){*t>32?1+e(t+1):0;}
f(){size_t l;char*t;getline(&t,&l,stdin)+1&&e(t)&&f();}

The above answer will read full lines, and check if the read line has content beside spaces\tabs.

shorter non-conforming version, 23 bytes

f(){getchar()-10?:f();}
\$\endgroup\$
1
\$\begingroup\$

Java, 79 bytes

boolean f(){System.console().readLine().replaceAll("\\t| ","").isEmpty()&&f();}

The above answer will read full lines, and check if the read line has content beside spaces\tabs.

shorter non-conforming version, 38 bytes

void f(){if(System.in.read()==10)f();}
\$\endgroup\$
1
\$\begingroup\$

VBScript, 26 bytes

while InputBox("")="" 
wend

This one is sort of fun because the only way to stop the programs asking for an answer is either giving one or killing 'wscript.exe' in the task manager.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.