0

On the second iteration of the for loop, there seems to be a null character that is preventing me from inserting my input. Is there a way to overcome this?

# include <stdio.h>

struct student
{
    char name[10];
    int score;
};

int main()
{
    int size = 5;
    struct student arr[size];

    for (int i = 0; i<size; i++ )
    {
        printf("[%d]# Please enter your name ... ",i+1);
        fgets(arr[i].name,10, stdin);

        printf("\n[%d]# Please insert your score ... ",i+1);
        scanf("%d",&arr[i].score);

        system("CLS");
    }
    return 0;
}
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
m16u31
  • 1
  • See also https://stackoverflow.com/questions/9537072/properly-using-sscanf – Richard Chambers Feb 12 '21 at 14:29
  • Your second scanf leaves a new line `'\n'` character in the standard input (stdin) when you type the score input, the basic solution is to discard it using the specifier `%*c` to read and discard a character, so the scanf would be `scanf("%d%*c",&arr[i].score);` . – isrnick Feb 12 '21 at 14:30
  • And the `system` function belongs to the `stdlib.h` library so you should include it `#include ` . – isrnick Feb 12 '21 at 14:34

0 Answers0