简体   繁体   English

在 c 中 fflush 时程序突然结束

[英]abrupt end of program while fflush in c

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);
}

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM