abrupt end of program while fflush in c

45 Views Asked by At

So I was doing an assignment for a class in which I have to perform basic array functions for an array of structures and in taking input my program closed on its own. The program terminated after taking input of name

void input(struct record *d){
printf("\nenter name: ");
fflush(stdin);
gets(d->name);
printf("\nenter adress: ");
fflush(stdin);
gets(d->adress);
printf("\nEnter mobile no :");
scanf("%s",d->mobile);
printf("\nenter marks:");
scanf("%if",d->marks);
printf("\nenter cgpa: ");
scanf("%if",d->cgp);
}
1

There are 1 best solutions below

1
On

The record structure must be initialized, and you should use

 scanf("%if", &(d->marks));
  printf("\nenter cgpa: ");
  scanf("%if", &(d->cgp));
#include <stdio.h>

typedef struct record
{
  char name[255];
  char adress[2048];
  char mobile[80];
  int marks;
  int cgp;
} record;

void input(struct record *d)
{
  printf("\nenter name: ");
  fflush(stdin);
  gets(d->name);
  printf("\nenter adress: ");
  fflush(stdin);
  gets(d->adress);
  printf("\nEnter mobile no :");
  scanf("%s", d->mobile);
  printf("\nenter marks:");
  scanf("%if", &(d->marks));
  printf("\nenter cgpa: ");
  scanf("%if", &(d->cgp));
}

void print_record(record *r)
{
  printf("Name: %s\r\n", r->name);
  printf("Adress: %s\r\n", r->adress);
  printf("Mobile %s\r\n", r->mobile);
  printf("Marks %i\r\nf", r->marks);
  printf("Cgp %if\r\n", r->cgp);
}

int main()
{

  record r1;
  input(&r1);
  print_record(&r1);
}