免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2190 | 回复: 4
打印 上一主题 下一主题

我打的这个程序哪里有问题,照着书打的 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-01 22:12 |只看该作者 |倒序浏览
  1. * booksave.c -- 把结构内容保存到文件中 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define MAXTITL 40
  5. #define MAXAUTL 40
  6. #define MAXBKS 10                /* 图书的最多本书 */
  7. struct book {                    /* 建立book模板 */
  8.   char title[MAXTITL];
  9.   char author[MAXAUTL];
  10.   float value;
  11. };

  12. int main(void)
  13. {
  14.   struct book library[MAXBKS];  /* 结构数组 */
  15.   int count = 0;
  16.   int index, filecount;
  17.   FILE *pbooks;
  18.   int size = sizeof(struct book);
  19.   
  20.   if ((pbooks = fopen("book.dat", "a+b")) == NULL)
  21.     {
  22.       fputs("Can't open book.dat file\n", stderr);
  23.       exit (1);
  24.     }
  25.   rewind(pbooks);                /* 定位到文件开始处 */
  26.   while (count < MAXBKS && fread(&library[count], size, 1, pbooks) == 1)
  27.     {
  28.       if (count == 0)
  29.         puts("Current contents of book.dat: ");
  30.       printf("%s by %s: $%.2f\n", library[count].title,
  31.              library[count].author, library[count].value);
  32.       count++;
  33.     }
  34.   filecount = count;
  35.   if (count == MAXBKS)
  36.     {
  37.       fputs("The book.dat file is full.", stderr);
  38.       exit (2);
  39.     }
  40.   
  41.   puts("Please add new book titles.");
  42.   puts("Please [enter] at the start of a line to stop.");
  43.   while (count < MAXBKS && gets(library[count].title) != NULL && library[count].title != '\0')
  44.     {
  45.       puts("Now enter the author.");
  46.       gets(library[count].author);
  47.       puts("Now enter the value.");
  48.       scanf("%f", &library[count++].value);
  49.       while (getchar() != '\n')
  50.         continue;               /* 清空输入行 */
  51.       if (count < MAXBKS)
  52.         puts("Enter the next title.");
  53.     }
  54.   if (count > 0)
  55.     {
  56.       puts("Here is the list of your books: ");
  57.       for (index = 0; index < count; index++)
  58.         printf("%s by %s: $%.2f\n", library[index].title,
  59.                library[index].author, library[index].value);
  60.       fwrite(&library[filecount], size, count - filecount, pbooks);
  61.     }
  62.   else
  63.     puts("No books? Too bad.\n");
  64.   
  65.   puts("Bye.\n");
  66.   fclose(pbooks);
  67.   
  68.   return 0;
  69. }
复制代码
  while (count < MAXBKS && gets(library[count].title) != NULL && library[count].title != '\0')
    {
      puts("Now enter the author.");
      gets(library[count].author);
      puts("Now enter the value.");
      scanf("%f", &library[count++].value);
      while (getchar() != '\n')
        continue;               /* 清空输入行 */
      if (count < MAXBKS)
        puts("Enter the next title.");
    }



我照着书打的,在红色部分时,输入空行不能跳出循环。。我哪里打出问题了?

论坛徽章:
0
2 [报告]
发表于 2011-12-02 08:31 |只看该作者
没break?

论坛徽章:
0
3 [报告]
发表于 2011-12-02 10:02 |只看该作者
回复 2# Ray001


    判断里有libaray[count].title != NULL和library[count].title != '\0'。如果直接敲回车,那应该不进行循环的啊

论坛徽章:
0
4 [报告]
发表于 2011-12-02 11:01 |只看该作者
library[count].title  是你的结构体里面的 char title[MAXTITL]; 的数组名,是个地址。你换成这个试试:
while (count < MAXBKS && gets(library[count].title) != NULL && library[count].title[0] != '\0')
{
  //...
}

论坛徽章:
0
5 [报告]
发表于 2011-12-02 14:12 |只看该作者
回复 1# sxmx1111


多写一写代码就好了

你这个主要还是自己写代码写得少

不懂得调试技巧

你把while (getchar() != '\n') contine;

换成while (getchar() != '^') break;
           
试一试
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP