0

I want my program to ask for an expression, assign an inputted string to a variable 'exp' and then print the expression.

However I am having some trouble. I first tried using (read)

(princ "Enter a expression to be evaluated.")
(setf exp (read))
(princ exp)

However when i use this code, this happens.

Hello this is an expression            ;This is what I input
Enter a expression to be evaluated.HELLO
T

I then tried to use (read-line), but when I do this, I don't seem to be asked for an input at all.

(princ "Enter a expression to be evaluated.")
(setf exp (read-line))
(princ exp)

gets

Enter a expression to be evaluated.
T

The program just ends.

After some answers I have come up with this

(defun get-input (prompt)
  (clear-input)                     
  (write-string prompt)             
  (finish-output)                   
  (setf exp (read-line)))           

(get-input "Enter an expression: ")
(princ exp)

However when i run this the following happens

My first sentence                            ;My first input
Enter an expression: My second sentence      ;it then asks for input, i do so
My second sentence                           ;my second input is printed back at me
T
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
sam
  • 123
  • 2
  • 8

1 Answers1

4

This is kind of a FAQ.

Output can be buffered. Use FINISH-OUTPUT to make sure that the output has actually reached its destination.

READ reads Lisp s-expressions. It returns the corresponding data-structure. It's only useful when you enter a valid s-expression.

READ-LINE reads a line and returns a string.

Example:

* 
(defun ask (&optional (message "Input: "))
  (clear-input)           ; get rid of pending input
  (write-string message)  ;
  (finish-output)         ; make sure output gets visible
  (read-line))            ; read a line as a string

ASK
* (ask "Name: ")
Name: Rainer

"Rainer"
NIL

File p.lisp:

(defun get-input (prompt)
  (clear-input)
  (write-string prompt)
  (finish-output)
  (read-line))

(write-string (get-input "Enter a sentence: "))
(finish-output)

Output

* (load "/tmp/p.lisp")
Enter a sentence: foo is not a bar
foo is not a bar
T
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • What is wrong in my second piece of code so that it doesn't read anything when using read-line ? It just seems to read nothing and doesn't wait for the user to type anything – sam Nov 29 '14 at 16:00
  • @sam: make sure that the output is printed. Use FINISH-OUTPUT. If there is pending input in the input stream, READ-LINE will read it and return immediately. – Rainer Joswig Nov 29 '14 at 16:06
  • I'm not sure I'm doing it right, I've tried using finish-output but don't know if Im using it correctly, what is it meant to do? My program has the exact same issues. – sam Nov 29 '14 at 16:14
  • This has helped a lot. There is ONE thing that's still broken though, when I run this, I have to input something and press enter before it then asks for the string. It then works as expected. But as I said, it first requires me to input something and press enter, this has no effect on the later output though. – sam Nov 29 '14 at 17:00
  • @sam: You would need to show us the code and a reproducible test case. – Rainer Joswig Nov 29 '14 at 17:02
  • I have added to the original question – sam Nov 29 '14 at 17:23
  • @sam: you need to declare variables. Other than that I can't reproduce your problem. See my edit. – Rainer Joswig Nov 29 '14 at 17:30