该程序输入有问题,还有其他一些问题

时间:2019-05-16 17:43:08

标签: c

我正在启动一个有效但无法正常工作的代码。当您输入三个名称时,程序将报告您回答以下三个问题,它们应该只是一个“名称”响应。首先,在将数据记录在文件中时,它将记录它们,但是当选择一个选项时,无尽的循环不会停止而是继续。我不知道该如何解决。

  char ID[8], name[20], address[10], IN[10];
  int flag = -1, books;
  int i, n, a, e;
  FILE *fp;
  if ((fp = fopen("catalogue", "wb")) == NULL) {
    printf("Error opening file for writing.\n");
    exit(1);
  }
  printf("Enter new ID:\n");
  scanf("%s", ID);
  i = strlen(ID); i++;
  printf("Enter new name:\n");
  scanf("%s", name);
  n = strlen(name); n++;
  printf("Enter new address:\n");
  scanf("%s", address);
  a = strlen(address); a++;
  printf("Enter new IN:\n");
  scanf("%s", IN);
  e = strlen(IN); e++;
  printf("Enter new number of books:\n");
  scanf("%d", &books);

  fwrite(&i, sizeof(int), 1, fp);
  fwrite(ID, i, 1, fp);
  fwrite(&n, sizeof(int), 1, fp);
  fwrite(name, n, 1, fp);
  fwrite(&a, sizeof(int), 1, fp);
  fwrite(address, a, 1, fp);
  fwrite(&e, sizeof(int), 1, fp);
  fwrite(IN, e, 1, fp);
  fwrite(&books, sizeof(int), 1, fp);
  fwrite(&flag, sizeof(int), 1, fp);
  fclose(fp);

  fp = fopen("catalogue", "rb");
  if ((fp = fopen("catalogue", "rb")) == NULL) {
    printf("Error opening file for reading.\n");
    exit(1);
  }
  fread(&i, sizeof(int), 1, fp);
  fread(ID, i, 1, fp);
  fread(&n, sizeof(int), 1, fp);
  fread(name, n, 1, fp);
  fread(&a, sizeof(int), 1, fp);
  fread(address, a, 1, fp);
  fread(&e, sizeof(int), 1, fp);
  fread(IN, e, 1, fp);
  fread(&books, sizeof(int), 1, fp);
  printf("ID: %s \n", ID);

  printf("Name:%s \n", name);

  printf("Address:%s \n", address);

  printf("Identification number:%s \n", IN);

  printf("Books:%d\n", &books);

  fclose(fp);

  int choice = 0;
  for (;;) {
    printf("  MENU\n");
    printf("   1.Add new reader\n");
    printf("   2.Search reader by books\n");
    printf("  3.Search reader by last name\n");
    printf("4.Delete a reader by ID\n");
    printf(" 5.Exit\n");

    do {
      printf("Enter your choice:\n");
      scanf("%d", &choice);
      fflush(stdin);
    }

    while (!(choice >= 1 && choice <= 5));
    printf("%d \n", choice);
 }

0 个答案:

没有答案