-1

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.

Geoff
  • 256
  • 2
  • 5
  • 20
  • 1
    Where do you initialize `count` to `0`? – kichik Apr 19 '13 at 22:56
  • 1
    `%c` is for characters, `inp` is an `int`. – David Schwartz Apr 19 '13 at 22:57
  • Oh, sorry, I do it right above the loop but I messed up on my copy/paste – Geoff Apr 19 '13 at 22:57
  • Although probably not the cause of your crash, `numbuff` is an array of 21 characters, yet you never check that `count` is in the range `0..20`. – marko Apr 19 '13 at 22:58
  • It's a known input that will never be larger than 20 characters but I see your point. – Geoff Apr 19 '13 at 22:59
  • @David Schwartz I was hopeful when you mentioned inp was an int so I changed it to a char but unfortunately, still the same issue. – Geoff Apr 19 '13 at 23:03
  • 1
    @Geoff You should keep it as int if checking for EOF, cast it when passed to printf instead. – Oskar N. Apr 19 '13 at 23:07
  • 1
    Is there any more code below the last line? The `printf("Out!")` is not necessarily printed where you expect because of standard output line buffering. – Oskar N. Apr 19 '13 at 23:09
  • just a return statement "return 1" – Geoff Apr 19 '13 at 23:18
  • 1
    @Geoff And what follows in the function you return to, or is this the `main` function of the program? If not, try putting `printf("Returned\n");` right after you call this function. – Arkku Apr 19 '13 at 23:24

2 Answers2

6

count is uninitialized which is used as array index. Initialize it to 0.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • I mentioned above in my comment that I do initialize count to 0 but I forgot to paste it in here. I will edit it. – Geoff Apr 19 '13 at 23:04
  • @Geoff I do not see any other problems with your code. Though you should use %d if you want to print an int and check not to overflow the buffer `numBuff`. But those are not the issues for your input. See here: http://ideone.com/TRfk6W I suspect that you do something else after the loop that's causing the problem. What's the rest of the look like? – P.P Apr 19 '13 at 23:06
3

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.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
  • I haven't learned how to use debuggers yet, that's next weeks agenda at school. – Geoff Apr 19 '13 at 23:19
  • Wow, amazing, it was happening outside of this loop, outside of the function entirely. There was a function call later in my main that was the actual problem. So you were right, it wasn't printing "Out!". Thanks a lot!! – Geoff Apr 19 '13 at 23:27
  • You're welcome. Remember - debugging is a lot easier if you don't blindly rely on assumptions but verify them. And realizing that when you eliminate the impossible what remains, however improbable, must be the truth. – Nik Bougalis Apr 19 '13 at 23:38
  • 1
    Occam's cutting tool to the rescue yet again :) – Michael Dorgan Apr 19 '13 at 23:44