I am working on implementing a brainfuck interpreter, and I’m struggling with the call of two consecutive , commands.
Here’s an extract of my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MEM_SIZE 30000
#define MAX_LINE_LENGTH 256
int main (int argc, char **argv)
{
char *input = ",.,.";
char bytes [MEM_SIZE] = {0};
int pos=0;
int i=0;
while (input[i] != '\0'){
switch (input[i]){
case '.':
printf ("%c", bytes[pos]);
break;
case ',':
printf ("Enter Number:\n");
bytes[pos] = fgetc (stdin);
printf ("Number Entered\n");
break;
default:
break;
}
i++;
}
return EXIT_SUCCESS;
}
The output of the program is the following:
Enter Number:
3 // This is me, manually entering the value.
Number Entered
3Enter Number:
Number Entered
Why isn’t the second call to fgetc working?
What makes you think the second call to
fgetcisn’t working ? The secondfgetcreads in an\n(you did pressreturn, right?).Another problem, you are reading into a char.
fgetcreturns anint. You should check this forEOFbefore blindly using it.