Search code examples
inputuser-inputbrainfuck

How is input handled in Brainf***?


I can't really seem to find a standard for this. I know inputs are taken as ASCII values, but are they required to be single characters? If not, how are multi-character inputs handled?


Solution

  • Command line inputs in most (if not all) programming languages are taken a line at a time. When you hit enter into a console after typing a line, the whole line gets sent into the program as a return value from the function you called to get the input. In brainfuck, you have more control over this: You can get as many characters as you want at a time, and stop when you want to. A single comma "," will get one byte's worth of input (a.k.a one character). If you want to handle getting a string until a newline is met, you can try implementing something like the following code (10 being the ascii value of newline and the number of repetitions of "+" and "-" chars):

    [-]>,----------[++++++++++>,----------]<[<]
    

    An array of non zero values starting and ending with zero values is saved into memory containing the ascii values of input chars.