When I run my program, fgets() is ignored and scan

2019-05-25 04:03发布

问题:

Whe I run it fgets() is simply ignored and scanf() doesn't input data to structure.

My code:

#include"stdio.h"
#include"stdlib.h"

typedef struct {
  char nazv[50];
  int numr;
  int date[3];
  int time[2];
} train;

void input(train*rasp,int n);
void print(train*rasp,int n);
void output(train*rasp, int n, int*date, int*time);

main() {
  int n;
  int t[2];
  int d[3];
  printf("How much? ");
  scanf("%d",&n);
  train* rasp=(train*)malloc(n*sizeof(train));
  input(rasp,n);
  printf("Enter date: ");
  for (int date=0;date<3;date++) {
    scanf("%d",&d[date]);
  }
  printf("Enter time: ");
  for (int time=0;time<2;time++) {
    scanf("%d",&t[time]);
  }
  for (int i=0;i<n;i++)
    print(rasp,n);
  output(rasp,n,d,t);
  free(rasp);
  return 0;
}
void input(train*rasp,int n) {
  for (int i=0;i<n;i++) {
    printf("Train #%d\n",i+1);
    printf("Enter train's name: ");
    fgets(rasp[i].nazv,50,stdin);
    printf("Enter train's number: ");
    scanf("%d",&rasp[i].numr);
    printf("Enter date: ");
    for (int d=0;d<3;d++) {
      scanf("%d",&rasp[i].date[d]);
    }
    printf("Enter time: ");
    for (int t=0;t<2;t++) {
      scanf("%d",&rasp[i].time[t]);
    }
  }
  printf("\nOK.");
}
void print(train*rasp,int n) {
  printf("Name: ");
  printf("%s",rasp[n].nazv);
  printf("\nNumber: %d",rasp[n].numr);
  printf("\nDate: %d.%d.%d", rasp[n].date[0],rasp[n].date[1],rasp[n].date[2]);
  printf("\nTime: %d:%d\n", rasp[n].time[0],rasp[n].time);
}
void output(train*rasp,int n,int*date,int*time) {
  for (int i=0;i<n;i++) {
    if (rasp[n].date[2]<date[2])
      continue;
    if (rasp[n].date[2]>date[2]) {
      print(rasp,n);
    } else {
      if (rasp[n].date[1]<date[1])
    continue;
      if (rasp[n].date[1]>date[1]) {
    print(rasp,n);
      } else {
    if (rasp[n].date[0]<date[0])
      continue;
    if (rasp[n].date[0]>date[0]) {
      print(rasp,n);
    } else {
      if (rasp[n].time[0]<time[0])
        continue;
      if (rasp[n].time[0]>time[0]) {
        print(rasp,n);
      } else {
        if (rasp[n].time[1]<=time[1])
          continue;
        print(rasp,n);
      }
    }
      }
    }
  }
}

input() function should put trains into to database. output() function should only print trains from database with a date later than the one user supplied before.

回答1:

scanf("%d",&n);

scanf leaves the newline that sent the input to the programme in the input buffer.

train* rasp=(train*)malloc(n*sizeof(train));
input(rasp,n);

void input(train*rasp,int n) {
  for (int i=0;i<n;i++) {
    printf("Train #%d\n",i+1);
    printf("Enter train's name: ");
    fgets(rasp[i].nazv,50,stdin);

fgets finds a newline as the first character in the input buffer and doesn't look further, leaving the train's name in the input buffer.

    printf("Enter train's number: ");
    scanf("%d",&rasp[i].numr);

Which scanf fails to convert into an int.

Mixing scanf and fgets for user input is error-prone becaus of the different newline-behaviour. Unless you clear the input buffer between a scanf and a following fgets, the fgets most often will only get the newline left over form the scanf (there are a few scanf formats that consume the newline).

You can clear the buffer

void clear_input_buffer(void) {
    int c;
    while((c = getchar()) != EOF && c != '\n');
    if (c == EOF) {
        // input stream broken, what now?
        exit(EXIT_FAILURE);
    }
}

or use fgets for all input and use sscanf (and strtol and friends) to parse the input.