Why is my Hello World Program failing?
문제
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:
+++++[>
+++++++++++++++++[>++++>++++>++++>++++>++>+++++>++++>++++>++++>++++<<<<<<<<<<-]
>++++.>+.>++++++++..>+++++++++++.>--.>++.>+++++++++++.>++++++++++++++.>++++++++.>.
[-]<[-]<[-]<[-]<[-]<[-]<[-]<[-]<[-]<[-]<[-]
<-]
or use this (don't print the array as you fill it in, wait until it is created and then do it)
+++++++++++++++++[>++++>++++>++++>++++>++>+++++>++++>++++>++++>++++<<<<<<<<<<-]
>++++>+>++++++++>+++++++++++>-->++>+++++++++++>++++++++++++++>++++++++>
<<<<<<<<<<
+++++[>.>.>..>.>.>.>.>.>.>.<<<<<<<<<<-]