-2

I wrote the following code:

int VALUE = 10;
int counter = 0;
char c;
while (true) {
    if(scanf("%c", &c) != 1) {
        return NULL;
    }
    if (c == '\n') {
        break;
    }
    if (counter % VALUE < VALUE - 1) {
        num[counter] = c;
        counter++;
    } else {
        char* temp = (char*) malloc((counter + MAX_VALUE)*sizeof(char));
        if (temp == NULL) {
            return NULL;
        }

        for (int i = 0; i < counter; i++) {
            temp[i] = num[i];
        }
        free(num);
        num = temp;
        num[counter] = c;
        counter++;
    }
}
printf("counter = %d\n",counter);

It doesn't really matter what it does but I have some problem with the counter. For some reason, when I insert 9876.54321 (newline at the end), It does not enters into the if (c == '\n') { break; } block when it scanfs \n, only on the next iteration. The length of 9876.54321 is 10 but it will print 11. What is the reason? I also tried to switch to getchar() but I get the same thing.

vesii
  • 2,760
  • 4
  • 25
  • 71
  • It might be carriage return symbol goes first, as `\r\n`.Try `c == '\n' || c == '\r'` – Renat Aug 07 '19 at 22:46
  • 1
    @Renat No, it's a `scanf` limitation. – S.S. Anne Aug 07 '19 at 22:47
  • I tested your code and it gave me 10 when I put 9876.54321 and it does enter into the if (c == '\n') { break; } – phoenixstudio Aug 07 '19 at 22:51
  • 1
    What is`num`? Where/how is it defined? – wildplasser Aug 07 '19 at 22:52
  • 2
    Please include a [mcve]. – S.S. Anne Aug 07 '19 at 22:52
  • 1
    Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). The usual way to solve this is to rewrite `scanf("%c", &c)` as `scanf(" %c", &c)` where the added space instructs `scanf` to filter out leading whitespace, if any. Most other format specifiers (`%d`, `%s` etc) do this automatically. It's bad practice to assume that there is always a newline to remove: the newline is not specifically considered or needed by `scanf` and may not always be present. – Weather Vane Aug 07 '19 at 23:32

1 Answers1

3

Here:

if(scanf("%c", &c) != 1) {

%c in scanf leaves the leading and following whitespace in the input buffer.

To fix this, don't use scanf. Use getchar or getc instead. Instead of using c == '\n', use isspace(c).

Don't do char c. You can't check for EOF if you do that. Instead, use int c.

Full code sample:

int VALUE = 10;
int counter = 0;
int c;
while (true) {
    c = getchar();
    if (isspace(c) || c == EOF) {
        break;
    }
    if (counter % VALUE < VALUE - 1) {
        num[counter] = c;
        counter++;
    } else {
        char* temp = (char*) malloc((counter + MAX_VALUE)*sizeof(char));
        if (temp == NULL) {
            return NULL;
        }

        for (int i = 0; i < counter; i++) {
            temp[i] = num[i];
        }
        free(num);
        num = temp;
        num[counter] = c;
        counter++;
    }
}
printf("counter = %d\n",counter);
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76