So, I handwrote this program in BrainFuck:

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

and it displays exactly what I wanted:

HELLO WORLD

But then I wanted it to display HELLO WORLD five times in a row, so I wrapped it in a loop:

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

I expected it to have this output:

HELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLDHELLO WORLD

But it's displaying this instead:

HELLO WORLDŠ˜˜ž@®ž¤˜ˆØÏääí`íöäÌ 00<€\<H0hY||‹ ³‹š|T

What am I doing wrong with my loop? Why isn't it displaying HELLO WORLD five times in a row, as expected?

有帮助吗?

解决方案

You aren't looping over "HELLO WORLD"' you're looping over the code that creates it...

You have two options, either zero everything out as you move back using [-]<, that works with your current code because it will return the array elements to thier initial state, and when you rerun your code it will be have the same values, i.e:

http://ideone.com/GjAp8

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

or use this (don't print the array as you fill it in, wait until it is created and then do it)

http://ideone.com/iWs8X

+++++++++++++++++[>++++>++++>++++>++++>++>+++++>++++>++++>++++>++++<<<<<<<<<<-]
>++++>+>++++++++>+++++++++++>-->++>+++++++++++>++++++++++++++>++++++++>
<<<<<<<<<<
 +++++[>.>.>..>.>.>.>.>.>.>.<<<<<<<<<<-]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top