1

So, I have the code below where I need to concatenate the first 'N' characters from str2 into str1. But IDK why when I type the str1 he automatically jumps to reading N and skips the str2 reading, str1 and str2 reading is exatcly the same.

#include <stdio.h>
#include <string.h>

int main(void) {
  char str1[50], str2[25];
  int n, tam;

  printf("Type the first string: ");
  scanf("%[^\n]s", str1);
  printf("Type the second string: ");
  scanf("%[^\n]s", str2);
  printf("Type N: ");
  scanf("%d", &n);
  tam = strlen(str1);

  for(int i=0; i<n; i++){
    tam++;
    str1[tam] = str2[i];
  }

  printf("Final string: %s\n", str1);

  return 0;
}

heres what happening

  • Does this answer your question? [Scanf skips every other while loop in C](https://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c) – ggorlen Jul 19 '21 at 15:54
  • 1
    Its been asked a lot on here https://stackoverflow.com/questions/29775323/scanf-function-seems-to-be-skipped-in-c – stefan_aus_hannover Jul 19 '21 at 15:55
  • Advice: Do not use the `%[...]` form with `scanf`. I know why someone told you to use it, but it's a bad idea in the long run. (In the long run, you want to move away from using `scanf` at all, for anything.) If you had used `%s`, `%s`, and `%d` to read your first string and your second string and your number, it would have worked just fine. – Steve Summit Jul 19 '21 at 16:22

2 Answers2

0

Change

  printf("Type the first string: ");
  scanf("%[^\n]s", str1);
  printf("Type the second string: ");
  scanf("%[^\n]s", str2);

to

  printf("Type the first string: ");
  scanf("%[^\n]s", str1);
  getchar();
  printf("Type the second string: ");
  scanf("%[^\n]s", str2);

the problem is that the second scanf gets the input from the enter key after first scanf, so you need to reset the buffer.

fcocea
  • 16
  • 1
  • 1
    or scanf(" %[^\n]s", str2); without getchar(); – fcocea Jul 19 '21 at 16:00
  • 1
    The space before `%` is *designed* to filter any amount of whitespace (or none), but `getchar()` must read exactly one character, whether it's a single unwanted whitespace or needed data (no whitespace). It's a poor kludge. And the `s` in `%[^\n]s` is the typical newbie mistake: `%[]` is a distinct format from `%s` with different behaviour. – Weather Vane Jul 19 '21 at 16:13
-1

You need to consume the newline (\n) that's generated by pressing enter.

It should be solved by adding one scanf just to consume it.

#include <stdio.h>
#include <string.h>

int main(void) {
  char str1[50], str2[25];
  int n, tam;

  printf("Type the first string: ");
  scanf("%[^\n]s", str1);
  scanf("%*c");
  printf("Type the second string: ");
  scanf("%[^\n]s", str2);
  scanf("%*c");
  printf("Type N: ");
  scanf("%d", &n);
  tam = strlen(str1);

  for(int i=0; i<n; i++){
    tam++;
    str1[tam] = str2[i];
  }

  printf("Final string: %s\n", str1);

  return 0;
}

The %*c will consume the newline character \n without storing it anywhere.

Vasconcelos
  • 300
  • 2
  • 9