I have been struggling with this for over an hour now and I can't seem to find out why I get this error.
int inp, count;
char numBuff[21];
count = 0;
while((inp=getchar()) != EOF) { // get Value (last field)
printf("input is '%c'\n", inp);
if (inp == '\n') break;
if (inp == ' ') {
continue;
}
numBuff[count++] = inp;
printf("go back through loop\n");
}
printf("Out!");
numBuff[count] = '\0';
if I input 1013 I get the following
input is '1'
go back through loop
input is '0'
go back through loop
input is '1'
go back through loop
input is '3'
go back through loop
input is '
'
Segmentation fault (core dumped)
The only thing I can gather from this is that it is failing when I check if inp == '\n' but why? I moved the go back through loop printf to just after the check if inp == '\n' and it never reached that one either so I know that it is occurring there.
You gather that the error is in the loop, but how do you know? Did you try using a debugger - a useful tool that will help pinpoint where a crash occurs and allow you to examine the state of your program.
Dollars to donuts the crash occurs in code after the loop. The reason you do not see the "Out" message is because you don't print a newline so the standard library buffers the output.