1

I'd like to handle user input in dc.

For ?, info dc indicates:

'?'                                                                                                                                                   
    Reads a line from the terminal and executes it.  This command                                                                                    
    allows a macro to request input from the user.

However, if I type this command sequence to square a number, followed by my input 9:

?d*p                                                                                                                                                  
9                                                                                                                                                     

I get:

dc: stack empty                                                                                                                                       
dc: stack empty                                                                                                                                       
dc: stack empty 

Am I misinterpreting what ? does?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Yimin Rong
  • 913
  • 2
  • 9
  • 23
  • What you need to realize is that when you give the command sequence `?d*p` followed by your input, say 9, then that 9 is placed after the `p` so that when dc executes d the stack is essentially empty. You can confirm this by giving the command seq `?zp` where `z` is the number of stack elements. You should see a zero proving that your input came after the p. Thence those stack empty messages that you are seeing. –  Jul 29 '17 at 15:39

1 Answers1

4

Assuming you are running this dc code on the command line, then after the ? provide the input the number to populate the stack on which dc undertakes it's operations.

?

3

d*p

Will result:

9

  • Then is the `?` even required? Would just `3␤d*p␤` do the same? – Yimin Rong Jul 29 '17 at 12:59
  • 1
    @YiminRong Now the 3 is hardcoded in the stack. `?` is very much needed if you need to do a input from the user. For example, using GNU dc, if we do: `dc -e '?d*p'` then run it, it will ask for input after which it will square it and return the results. –  Jul 29 '17 at 13:01