0

My fourth scanf is not taking input?

I'm trying to make mark sheet in c language. Then my first three scanf run properly but my fourth scanf is not taking input my program is directly stop executing.

#include<stdio.h>
int main(){
    int subject, mathematics, english, hindi, science, socialScience;
    char school[50];
    char name[50];
    char section[5];
    int standard;
    int total;

    printf("\nPlease Enter your school name: ");
    scanf("%[^\n]%*c", school);

    printf("\nEnter student name: ");
    scanf("%[^\n]%*c", name);

    printf("\nenter standard: ");
    scanf("%d", &standard);

    printf("\nenter your section:");
    scanf("%[^\n]%*c", section);
    
    return 0;
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • 2
    Does this answer your question? [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) – Gerhardh Jul 29 '22 at 08:21
  • 2
    The general advice around here when it comes to `scanf` is: Forget that it exists. Use `fgets` to read lines, and then possibly `sscanf` to parse the lines. – Some programmer dude Jul 29 '22 at 08:22
  • Side note: If you use `scanf` or `sscanf` etc., you should always check return value. Also, if you don't have a `\n` at the end of your output, you might not see it due to line buffering of `stdout`. If you don't want `\n` because you want to get the input in same line, you can use `fflush(stdout);` after your `printf` calls. – Gerhardh Jul 29 '22 at 08:23
  • Instead of trying to kludge away the newline with `scanf("%[^\n]%*c", school);` you must filter out any *leading* newline with `scanf(" %[^\n]", school);` with an added space. This is how the function is intended to be used. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Jul 29 '22 at 08:24
  • `scanf("%d", &standard);` - ask yourself what is left in the input stream immediately *after* that happens. Consider every keystroke you hit; *every single keystroke*. What did you hit after you finished the last digit of `standard` ? Now, what causes the next `scanf` to *stop* parsing its first argument? – WhozCraig Jul 29 '22 at 08:24
  • You must also restrict the input length, escpecially with a tiny buffer like `char section[5];` with `scanf(" %4[^\n]", section);` – Weather Vane Jul 29 '22 at 08:27
  • Begin again. You've already instantiated lots of variables when you encounter a problem. "Incremental Development"... As others have said, shun `scanf()` and use `fgets()` to load into a general purpose buffer that you can `strncpy()`. Get one field working before moving on to the next. Use `val = atoi(buffer)` instead of `scanf("%d")... – Fe2O3 Jul 29 '22 at 08:34

1 Answers1

0

Add a getchar() after the 3rd scanf(). But as other have suggested, please move to fgets().Check this answer for explanation

NeilB
  • 347
  • 2
  • 16