SuperForth for the Tatung Einstein, showing a SuperForth 1.12 session defining a word
← Back to Courses
module
19

Asking A Question

Introduction

KEY gets you one keypress. To ask "how many?" you need a whole number, typed at leisure and finished with ENTER. SuperForth has a word for it, in UTILITY.

#IN

Screen 4 of UTILITY defines #IN. It stops, waits for a line to be typed, and leaves what was typed on the stack as a number: ( -- n ).

In MAME, # is SHIFT and 3.

NEWFILE UTILITY
4 LOAD
#IN .

Type 123 and ENTER, and . prints 123.

The Rule About Rows

You will want to print a question first. Here is the obvious way, and it does not work:

: ASK ." HOW OLD ARE YOU? " #IN ;

Type 41 after the question and SuperForth answers HOW?.

S7 told you why, in another form: SuperForth reads the whole row of the screen that the cursor is on. It does not know which part of the row you typed and which part it printed itself. The row says HOW OLD ARE YOU? 41, so it tries to make a number out of HOW.

The cure is to end every prompt with CR, so that the answer is typed on a fresh row of its own.

The Code

NEWFILE UTILITY
4 LOAD
: ASK ." HOW OLD ARE YOU?" CR #IN ;
: OLDER ASK 1+
." NEXT YEAR " . ;
OLDER

Starting from

A fresh boot, with SuperForth just started. When it asks, type 41.

What you should see

OLDER
HOW OLD ARE YOU?
41
NEXT YEAR 42 ok

OLDER asks a question on one row and reads the answer from the next

When The Answer Is Not A Number

#IN is unforgiving. Type letters and you get them back with a ?, exactly as if you had typed them at SuperForth itself - and the word that was running is abandoned, stack and all. Press ENTER on an empty row and you get a bare ?.

There is no second chance built in. A program that must survive a careless answer has to ask with KEY and build the number itself, which is a good exercise for later and too much for now. For your own programs, a clear prompt is most of the battle.

Change One Thing

  • Put the question and the #IN on the same row by taking out the CR. Type a number. Which word does SuperForth complain about, and why that one?
  • Answer OLDER with -7. Does #IN accept negative numbers?
  • Answer with FORTY. What happens to the rest of OLDER?
  • Answer with 40000. What comes back, and which section explained it?

Exercises

19.1 Define DOUBLE-IT to ask for a number and print twice it.

19.2 Define ADD-UP to ask for two numbers and print their sum.

19.3 Define TABLE-FOR to ask which times table you want, and print it up to ten times. You wrote most of this in S12.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
The first word of your own question, with a ?The answer was typed on the same row as the prompt. Put CR after the prompt.
#IN?Screen 4 of UTILITY is not loaded.
A word with a ?, and your program stopsSomething other than a number was typed.
A strange negative numberThe answer was over 32767 and wrapped.

Summary

#IN, from screen 4 of UTILITY, waits for a typed line and leaves it as a number. It reads the entire screen row, so print the question, then CR, then ask. Anything that is not a number stops the program.

Next

S20, Chance - random numbers, a game built on them, and why SuperForth's dice always fall the same way until you do something about it.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.