+ 12
Help Me in this code! Why is it not working?
I have tried to make a simple brainf*** interpreter to take code and output its result... But it is not working and giving any output... Please Help!
12 Antworten
+ 15
0) Where did you get this code
1) Where did you try to run it on
2) What errors did you get
+ 13
@Hatsy
0) Made it myself
1) Ran it on CppDroid
2) No Errors, but no output on input:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
//Should Print 'A'
Or this:
++++++++[>+++++++++<-]>+.<++++++[>------<-]>-----.<++++++[>+++++++<-]>++.<+++++[>+++++++<-]>.+++++++.-----------------.<++++++++[>--------<-]>-----.<+++++++[>++++++++<-]>+.<++++[>+++++<-]>++.++++++.
+ 12
@Hatsy Rei
On running this in Code Playground, the compiler gives me a Compilation Error, which I am unable to find...
+ 12
@Babak Sheykhan (PERS)
Yes...
+ 12
@Ace
Can you please name one, sir?
The truth is that I have never used an online debugger... 😅
+ 12
@Ace
But what you meant was an automatic one, right sir?
Or you wanted me myself to find the errors - that I tried but couldn't find any...(Perhaps I don't have that much knowledge)...
+ 12
@Ace
Thank You, Sir!
+ 11
#include <iostream>
using namespace std;
class Brainfuck {
public:
char data[30000];
char *d;
const char *p;
Brainfuck(const char prog[]) {
d = data;
p = prog;
}
void pincr() {
d++;
}
void pdecr() {
d--;
}
void vincr() {
(*d)++;
}
void vdecr() {
(*d)--;
}
void puts() {
cout << *d;
}
void gets() {
cin >> *d;
}
void bropen() {
int bal = 1;
if (*d == '\0') {
do {
p++;
if (*p == '[') bal++;
else if (*p == ']') bal--;
} while ( bal != 0 );
}
}
void brclose() {
int bal = 0;
do {
if (*p == '[') bal++;
else if (*p == ']') bal--;
p--;
} while ( bal != 0 );
}
void evaluate() {
while (*p) {
switch (*p) {
case '>':
pincr();
break;
case '<':
pdecr();
break;
case '+':
vincr();
break;
case '-':
vdecr();
break;
case '.':
puts();
break;
case ',':
gets();
break;
case '[':
bropen();
break;
case ']':
brclose();
break;
}
p++;
}
}
;
int main(void) {
char* buffer;
buffer= new char [10000];
cin>>buffer;
Brainfuck interpreter(buffer);
interpreter.evaluate();
}
+ 4
Did you trace your code?