brainfuck

my brainfuck code wrap from 0 to 255 in the cells when I decrement values and make an error when the input is minor than 5


when I decrement a value in a check cell by 5 to determine if the value the user entered is greater or less than 5, two situations occur: 1) when the input is greater or equal than 5, the result is correct, and the program print that the input is greater than 5. when the input is less than 5, the cell underflow and go to 255 bits, therefore, the program define that the input is greater than 5 even when it is less.

>>>+<<< (Adds a check to cell 3 with state 1 and returns to cell 0)

++++++++++[>+++++<-]>+++.
(Add 53 bits (5 in ASCII) to cell 0; which will serve as a check mark)
(and then transfer the value to cell 1)

<,.>[<->-]<   
(Go back to cell 0)
(ask the user for the number and print it)
(move to the next cell (cell 1; the check cell))
(and start decrementing both cells until cell 1 is empty)

[ (loop ≠ 0) (only enter if there are leftovers)

    [-] (zeroing out cell 0 to avoid printing errors)
    ++++++++++[>+++++++<-]>+++++++.[-]< (M)
    ++++++++++[>++++++<-]>+++++.[-]<    (A)
    ++++++++++[>+++++++<-]>+++.[-]<     (I)
    ++++++++++[>++++++++<-]>-.[-]<      (O)
    ++++++++++[>++++++++<-]>++.[-]      (R)
    ("maior" and "menor" means bigger and smaller in portuguese)

    >>[-] (clean the cell 3 to avoid entering the next loop)
    <<<   (return to cell 0)
    [-]   (clean the cell 0 to avoid repeating this loop)

] (end of the loop)

>>> (Go to cell 3)
(If it's empty; it means the first loop worked; so the next one won't start)
(Otherwise; it will run anyway)

[ (second loop ≠ 0)

    [-] (clean the cell 3)
    ++++++++++[>+++++++<-]>+++++++.[-]<  (M)  
    ++++++++++[>++++++<-]>+++++++++.[-]< (E)  
    ++++++++++[>+++++++<-]>++++++++.[-]< (N)  
    ++++++++++[>++++++++<-]>-.[-]<       (O)  
    ++++++++++[>++++++++<-]>++.[-]<      (R)  
    [-] (Clear cell 3 so the loop doesn't repeat)

] (end of the loop)

The problem is that the program detects when the number is greater than 5, but it doesn't detect when it's less than 5. When the user put 4 the number underflows and wraps to 255 instead of remaining at 0. And the program says that the number is always greater than 5, except when I use the number 5.


Solution

  • From what i see, your loop is driven by the "5" cell, so when input < 5 you keep decrementing past 0 and it wraps to 255. Drive the loop with the input cell instead. Stop when input hits 0, then check what's left in the "5" cell.

    Tiny Brainfuck core (cell0 = numeric 0–9, cell1 = 5):

    <+++++        set cell1 = 5 (assumes youre on cell1; adjust moves as needed)
    <             back to input (cell0)
    
    [             while input > 0
      -           input--
      >-          five--
      <           back to input
    ]
    
    >             now on five
    [             if five > 0 => input < 5
      /* MENOR */
      [-]
    ]
    
    <             back to input
    >             go to five again
    <[            if five == 0 => input >= 5
      /* MAIOR */
    ]>
    

    If you're reading a digit, subtract 48 first to get 0–9, then try running this