0

Okay so I am writing this simple piece of code and I am just a beginner in C. This code compiles and runs as expected but when I get to the point where I must enter an address this code just skips the input, but when I use scanf in that place the code runs perfectly can anyone help me out?

SOURCE

#include<stdio.h>
#include<conio.h>

void main()
{
    struct student
    {
        int rn;
        long int phn;
        char name[50];
        char add[50];
    }s;

    char ans, filename[15];

    FILE *fp;
    printf("\n Enter File Name : ");
    gets(filename);
    fp=fopen(filename, "w");

    again:
    printf("\n Enter a name of a student : ");
    gets(s.name);
    printf("\n Enter his roll number : ");
    scanf(" %d ", &s.rn);
    printf("\n Enter his address : ");
    gets( s.add);
    printf("\n Enter his phone Number : ");
    scanf("%ld", &s.phn);
    fprintf(fp, "%s\t%d\t%s\t%ld\n", s.name, s.rn, s.add, s.phn);
    printf("\n Want to keep next record ? : ");
    fflush(stdin);
    scanf("%c", &ans);
    if(ans=='y'||ans=='Y')
    goto again;
    fclose(fp);
    getch();
}

I want a detailed explanation to this can anyone help me out?

Naveen
  • 105
  • 1
  • 9

0 Answers0