# Wait... what was I going to say again?

I often forget what I wanted to say when the teacher calls on me to speak. Can you make me a tool to solve this?

Requirements:

• The program must loop as follows:
• First, take in input
• If the input is empty, print the last stored question.
• Otherwise, store the input as a question.

Notes:

• The input will never be empty if there are no questions stored.

Clarification: The program does not need to store multiple questions. I originally wanted it to store multiple, but I changed it after many people misunderstood the question.

Remember, this is , so the code with the smallest number of bytes wins.

• Did you mean "previously stored question"? I find "last stored question" confusing as it suggests you might store more than one question.
– Neil
Commented Nov 6, 2016 at 19:58
• @Neil It's supposed to be storing more than one question, but since many answers already misunderstood the question, I'll just leave it be. Commented Nov 6, 2016 at 20:05

# Perl, 1317 14 +1 = 15 bytes

/./?$;=$_:say$ +1 for the -n flag (saved 4 bytes thanks to @Dada) As long as the input is not equal to the carriage return, it stores the input in $;. If it is equal to the carriage return, it prints what's already in $;. This assumes that all input can be characterized as questions, even if grammatically, they are not such. • You probably wanted to write ne instead of eq (right now this doesn't work). – Dada Commented Nov 6, 2016 at 9:26 • You can save three byte by doing /./ instead of $/ne$_ ;-) – Dada Commented Nov 6, 2016 at 9:28 # 05AB1E, 1211 10 bytes Thanks @daHugLenny and @Adnan for 1 byte! Thanks @Emigna for 1 byte! [IDõQiX,ëU  • I can't test now, but I think you can remove the last character. Commented Nov 6, 2016 at 2:02 • Yes, you can remove the last bracket (it will be auto-completed). Commented Nov 6, 2016 at 8:27 • If you replace ® and © with X and U, you don't need the backslash. Commented Nov 6, 2016 at 13:48 ## Haskell, 49 bytes g s=getLine>>=(#s) ""#s=putStr s>>g s l#s=g l g""  How it works: start with g "". The parameter s of function g is the question in store. g reads the next line from stdin and passes it (and also s) to #. If the line is empty, # prints the store and calls g again. If the line is not empty, g is called with the line as the new store. # JavaScript, 3634 31 bytes for(;;b?a=b:alert(a))b=prompt() An infinite loop keeps asking for input and stores it in b. It the input is not empty it's then stored in a, otherwise the value of a is printed. Saved 2 bytes thanks to @ETHproductions • Nice answer. You can save a byte by removing the 1, and another by moving b?a=b:alert(a) into the last section within the for-loop (i.e. for(a=0;;b?a=b:alert(a))b=prompt()) Commented Nov 6, 2016 at 3:52 • In fact a=0 is not necessary neither. Since the input can only be empty if there are questions stored, a won't be referenced if it hasn't been assigned. Commented Nov 6, 2016 at 4:08 • also stops the browser for a few seconds with firefox-49.0.2 Commented Nov 6, 2016 at 18:21 • The only requirement in your question is that empty input prints the last stored question. Why do you want me to hold references to previously stored questions? Commented Nov 6, 2016 at 18:25 • @bunyaCloven For me it only stops the browser when I tell it to block dialogs. That's expected due to the infinite loop. After some seconds the browser detects the problem and kills the JS. Commented Nov 6, 2016 at 18:28 # Mathematica, 44 bytes If[(b=InputString[])=="",Print@a,a=b]~Do~∞  Full program. The Unicode character is U+221E INFINITY for \[Infinity]. ## Python 3, 31 bytes while 1: I=input()or I;print(I)  Try it out! -1 byte thanks to Flp.Tkc • you can write that while loop on 1 line Commented Nov 7, 2016 at 8:11 # reticular, 12 bytes id""E[$dp]~*


Try it online!

## Explanation

id""E[$dp]~* i take a line of input [input] d""E push equality with "" [input, input == ""] [$dp]    push that func           [input, input == "", [$p]] ~* execute it iff equal on equal:$       drop empty input
dp     duplicate then print the TOS
this wraps around to the beginning, taking another line of input


## Pyke, 6 bytes

z=z~zr


Try it here!

     r - while 1:
z      -   input() or z.contents
=z    -  z.contents = ^
~z  -  z.contents


# Python3 - 49 bytes

s=""
while 1:
b=input()
if b:s=b
else:print(s)

• NameError: name 'b' is not defined Commented Nov 6, 2016 at 10:54
• @shooqie Ah, I typed this with my phone so I accidentally left out b=input(). Thanks! Commented Nov 6, 2016 at 11:29

## Python 3, 34 bytes

s=""
while[print(s)]:s=input()or s

• It doesn't? It works fine for me
– Blue
Commented Nov 6, 2016 at 18:09
• The spec doesn't ask to store multiple questions?
– Blue
Commented Nov 6, 2016 at 18:12

# Python 3, 60 bytes

q=[]
while 1:
s=input()
if''<s:q+=[s]
else:print(q.pop())


# CMD, 37 bytes

Sadly set does not take empty input:(

set/p a=
if %a%.=. echo b
set b=%a%
c


in a file names c.cmd or

set/p a=
if %a%.=. echo b
set b=%a%
%0


in any file. Working(Use - as empty):

set/p a=
if %a%.=-. echo b
set b=%a%
c

• set/p will never make a variable empty, so at the very least you need set a= on the first line; also I think you mean set b=%a%.
– Neil
Commented Nov 6, 2016 at 19:53

# APLX, 19 bytes

a←⍞⋄→(0=⍴a)/1⋄a⋄→1


### Explanation:

⍝ Note: the ⋄ symbols are statement separators, so I've replaced them here with newlines

a←⍞           ⍝ get an input string (⍞) and store it in variable a
→(0=⍴a)/1     ⍝ if the length of a (⍴a) is 0, goto (→) the start of this program (1)
a             ⍝ else, print a
→1            ⍝ goto the start of the program (1)


# C, 73 63 bytes

g(){char a[99],x;for(;;){gets(a);if(*a)x=*a;else*a=x,puts(a);}}


Only uses 1 buffer and a single char for handling the logic. If user inputs nothing, only the first char in the buffer gets overwritten by '\0', in that case it gets restored from x and printed, otherwise copy the first letter into x for later use.

Previous code:

main(){char a[99],b[99];for(;;){gets(a);if(*a)strcpy(b,a);else puts(b);}}


# Java 7, 189187 178 bytes..

class M{public static void main(String[]a){String s="",x;do{x=new java.util.Scanner(System.in).nextLine();if(x.isEmpty())Syste‌​m.out.println(s);els‌​e s=x;}while(1>0);}}


-9 bytes by using new java.util.Scanner directly, thanks to @cliffroot.

Probably the first time I used a do-while in a codegolf challenge..

When functions are allowed instead of programs, it's 135 bytes (still many times more than most other answers..)

• this is a a bit shorter and seems to be working – void c(){for(String s="",x;(x=new java.util.Scanner(System.in).nextLine())!=null;)if(x.isEmpty())System.out.println(s);else s=x;} Commented Nov 7, 2016 at 11:14
• @cliffroot Ummm.. aren't you missing a for or while somewhere?.. What is (x=new java.util.Scanner(System.in).nextLine())!=null;)? Commented Nov 7, 2016 at 12:32
• Yeah, I managed to miss for before that somehow :) it was supposed to be void c(){for(String s="",x;for(x=new java.util.Scanner(System.in).nextLine())!=null;)if(x.isEmpty())System.out.println(s);else s=x;} and it's actually 1 byte longer then the version with do-while is – void c(){String s="",x;do{x=new java.util.Scanner(System.in).nextLine();if(x.isEmpty())System.out.println(s);else s=x;}while(1>0);} Commented Nov 7, 2016 at 12:35
• @cliffroot Ah of course.. thanks! Commented Nov 7, 2016 at 12:48

# sed, 10 bytes

/./{x;d};g


Explanation:

      (implicit at start of program) read input, overwriting old input
/./{  If the input is nonempty
x;  swap the input with the hold space
d   then restart, without any implicit printing of input
};    end if statement
g     Append the hold space to the [empty] input
(implicit at end of program) print the input, then restart


Nice to see an exoteric (i.e. "not esoteric") language tying with some of the golfing languages!