0

In the code below, after the scanf() for newMIn it skips the gets() for newLname. I've tried relocating it to other parts but it still skips, I've also tried using getchar() instead of scanf() but it still skips the next immediate function. All other variables are strings, and this function is part of a larger program.

void getINF(char *newStID, char *newLname, char *newMIn,char *newFname, char *newHomeAdd, char *newCourse, char *newMumNam,char *newPopsNam  ){

    printf("Input Student ID:\n");
    gets(newStID);
    printf("Input First Name:\n");
    gets(newFname);
    printf("Input Middle Initial:\n");
    scanf(" %c", &newMIn);
    printf("Input Last Name:\n");
    gets(newLname); 
    printf("Input Home Address:\n");
    gets(newHomeAdd);
    printf("Input Course:\n");
    gets(newCourse);
    printf("Input Name of Mother:\n");
    gets(newMumNam);
    printf("Input Name of Father:\n");
    gets(newPopsNam);

    printf("\n\n%s\n",newStID);
    printf("%s\n",newFname);
    printf("%c\n",newMIn);
    printf("%s\n",newLname);
    printf("%s\n",newHomeAdd);
    printf("%s\n",newMumNam);
    printf("%s\n",newPopsNam);
    printf("%s\n",newCourse);

    return;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
MrKabuki
  • 7
  • 2
  • 1
    First, never use the unsafe `gets()` function. It was deprecated in C99 and completely removed from the language in C11. Second, this may be the most common question on SO. The `scanf()` function is leaving a newline character behind in the input stream which is interfering with the call to `gets()`. You need to remove the newline from the input stream. Better, don't mix `scanf()` with other input functions. – ad absurdum Feb 24 '18 at 02:10
  • So I should just either use fgets() or scanf() on their own? – MrKabuki Feb 24 '18 at 02:21
  • 1
    One solution that is often convenient is to use `fgets()` to fetch user input, then `sscanf()` to parse the input. You could also use `strtol()` to convert the string obtained from `fgets()` to an integer. Alternatively, you may be able to stick with `scanf()` everywhere. Most `scanf()` conversion specifiers automatically skip leading whitespace (except for `%c`, `%[]`, and `%n`), so the extraneous newline is not a problem. – ad absurdum Feb 24 '18 at 02:27

2 Answers2

1

When using the %c token with scanf, it will not consume any extra whitespace of newline characters. As such, the newline character after pressing enter is left in the input buffer and then consumed by the following gets call.

0

Use this popular method to clear the input before the next newline:

int c;
while ((c = getchar()) != '\n' && c != EOF);
iBug
  • 35,554
  • 7
  • 89
  • 134
  • If this is simple... another reason to avoid `scanf()`. Or just use the idiomatic and reliable `int c; while ((c = getchar()) != '\n' && c != EOF) { continue; }`. – ad absurdum Feb 24 '18 at 02:31
  • @DavidBowling That's the other I came up with but it's more characters to type (on a 5.5" phone). Anyway, thanks for the suggestion. – iBug Feb 24 '18 at 02:32
  • Good thing we don't have to write code on our phones, yet... ;) – ad absurdum Feb 24 '18 at 02:33
  • 1
    `scanf("%*[^\n]%*c");` fails to consume anything if the first character is a `'\n'`. Recommend to not use that. – chux - Reinstate Monica Feb 24 '18 at 04:17