phyrrus9 phyrrus9 - 4 months ago 16
Brainfuck Question

Cannot figure out if statement

Here is the code i am working with, it tests for the input of 'b' and should output 'Y' if there was anything else inputted.

Please, nobody ask why I chose to use 'Y' for a false value..

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


[
>
+++++ +++++
[
> +++++ +++
< -
]
> +++++ ++++ <
. <
]


Would like the equivalent of

char c;
c = getchar();
if (c == 'b')
putchar('Y');


I am getting no output (directed it at a file and hexdumped it, as well as nothing on the screen) no matter what I put into the program

Answer

Your issue is that when you reach your second loop you always have a value of zero in the current cell, so your second loop never runs. You need to be one further cell to the right to get the result of your modifications to the input, and then you need to do work only if the value is zero (unlike loops, which will only do work if the value is not zero).

To make a conditional I would suggest grabbing something from a site with some code someone else has come up (an example I found).

At any rate, this is the code I came up with to solve your problem:

,

7 * 7 * 7 = 98 = 'b'
> +++++ ++
[ > +++++ ++
  [
    << --
    >> -
  ]
  < -
]
<

expect everything to be zero here

[ >+ >+ <<- ]
> [ <+ >- ] +
> [ <- >[-] ]
< [ <
  >> [-]
  (11 * 8) plus 1 = 89 = 'Y'
  +++++ +++++ +
  [ << +++++ +++ >> - ]
  << + .
  > -
]
Comments