0
int main (){
    int id[ms],qid,score[ms],oscores[ms],menu,age[ms],nstud,i, dob[ms],a;
    float stime[ms],ttime1[ms], cyctime[ms],ttime2[ms],runtime[ms],otime[ms];
    float koswin = 0, ikidswin = 0, ciwin = 0, swimwin = 0, cycwin = 0, runwin =0;
    char name[100][ms], sc[100][ms], etype[ms], gender [ms], kwin, iwin, 
    cwin,word[100],pname[100],psc[100];
    FILE *fh;
    FILE *fp;
    fh=fopen("participants.txt","a");

    printf("Would you like to:\n");
    printf("1.Register participants\n");
    printf("2.Access a participant's details\n");
    scanf("%d",&menu);
    system("cls");

    if (menu == 1)
    {
        printf("Please enter number of participants you would like to register\n");
        scanf("%d",&nstud);
        for (i = 0; i < nstud; i++){
            printf("Please enter the the ID number of the participant:\n");
            scanf("%d",&id[i]);
            printf("Please enter the participant's name:\n");
           fgets(pname, sizeof(pname), stdin);
            strcpy(sc[i], pname);
            printf("Please enter the participant's School/Club:\n");
            fgets(psc, sizeof(psc), stdin);
            strcpy(sc[i], psc);
            
        }
        for (i= 0; i < nstud; i++ )
        fprintf(fh,"\n%d\t\n%s\t",id[i],name[i]);
        fclose(fh);

    }

The code works up until I am to enter the name it skips over the name and goes to the school input but it doesn't even accept the school name. It continues by skipping another input. I first had scanf("%s", name[i]) and scanf("%s", sc[i]) and that worked perfectly but I wanted the user to be able to enter a space. So is there a way to accept a string that is apart of a parallel array with space and write it to a file?

1 Answers1

0

The entry of the name looks skipped because the previous scanf() for the entry of ID leaves the newline character in the input buffer and the fgets() for the name reads it. To avoid this, modify the line:

scanf("%d",&id[i]);

to

scanf("%d%*c",&id[i]);

to flush the input buffer.

tshiono
  • 21,248
  • 2
  • 14
  • 22
  • Note: `"*c"` in `"%d%*c"` consumes the next character after the number - it _might_ be a `'\n'`. `scanf("%d%*c",&id[i]);` consumes nothing if the first part is not numeric text. – chux - Reinstate Monica Mar 14 '22 at 00:04