关于C Primer Plus 一个结构体数组的例子

EmbeddedLong 2012-02-16 11:55:55
#include<stdio.h>
#define MAXTITLE 2 //标题
#define MAXAUTL 2 //作者
#define MAXBKS 2 //最多可以容纳图书册数

struct book //建立book模板
{
char title[MAXTITLE];
char author[MAXAUTL];
float value;
};

int main(void)
{
struct book library[MAXBKS];
int count=0;
int index;

printf("please enter the books title.\n");
printf("please [enter] at the start of aline to stop.\n");
while(count<MAXBKS && gets(library[count].title)!=NULL && library[count].title[0]!='\0')
{
printf("Now enter the book author.\n");
gets(library[count].author);

printf("Now enter the value.\n");
scanf("%f",&library[count++].value);

while(getchar()!='\n')
continue;

if(count<MAXBKS)
printf("Enter the next title.\n");

}

if(count>0)
{
printf("Here is the list of your books:\n");
for(index=0;index<count;index++)
printf("%s by %s: $ %.2f \n",library[index].title,library[index].author,library[index].value);

}
else
printf("No book? Too bad!");

return 0;

}


它的输出是;
lease enter the books title
please [enter] at the start of aline to stop

111
Now enter the book author
kkk
Now enter the value
12


Enter the next title
222
Now enter the book author
eee
Now enter the value
32

Here is the list of your books
11kk by kk: $ 12.00 应该是;111 by kkk &12.00
22ee by ee: $ 32.00 222 by eee &32.00

小弟没看懂我是 也单步看了下,没看出来为什么。
...全文
90 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
EmbeddedLong 2012-02-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sagegz 的回复:]
C/C++ code

#include<stdio.h>
#define MAXTITLE 4 //这里
#define MAXAUTL 4 //这里
#define MAXBKS 2 //最多可以容纳图书册数

struct book //建立book模板
{
char title[MAXTITLE];
char author[MAXAUTL]……
[/Quote]

我实验过 可以这个 能说说原因吗?谢谢
EmbeddedLong 2012-02-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yht8708 的回复:]
#define MAXTITLE 5 //标题
#define MAXAUTL 5 //作者
试试 是不是能达到效果
[/Quote]
可以啊 能达到最大效果的
yht8708 2012-02-17
  • 打赏
  • 举报
回复
#define MAXTITLE 5 //标题
#define MAXAUTL 5 //作者
试试 是不是能达到效果
sagegz 2012-02-17
  • 打赏
  • 举报
回复

#include<stdio.h>
#define MAXTITLE 4 //这里
#define MAXAUTL 4 //这里
#define MAXBKS 2 //最多可以容纳图书册数

struct book //建立book模板
{
char title[MAXTITLE];
char author[MAXAUTL];
float value;
};

int main(void)
{
struct book library[MAXBKS];
int count=0;
int index;

printf("please enter the books title.\n");
printf("please [enter] at the start of aline to stop.\n");
while(count<MAXBKS && gets(library[count].title)!=NULL && library[count].title[0]!='\0')
{
library[count].title[MAXTITLE-1] = '\0'; //输出字符串得在字符数组末尾加'\0'
printf("Now enter the book author.\n");
gets(library[count].author);
library[count].author[MAXAUTL-1] = '\0';
printf("Now enter the value.\n");
scanf("%f",&library[count++].value);

while(getchar()!='\n')
continue;

if(count<MAXBKS)
printf("Enter the next title.\n");

}

if(count>0)
{
printf("Here is the list of your books:\n");
for(index=0;index<count;index++)
printf("%s by %s: $ %.2f \n",library[index].title,library[index].author,library[index].value);

}
else
printf("No book? Too bad!");

return 0;

}
赵4老师 2012-02-17
  • 打赏
  • 举报
回复
VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)”

提醒:
“学习用汇编语言写程序”

“VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)”
不是一回事!

不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。
并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。
任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!
赵4老师 2012-02-17
  • 打赏
  • 举报
回复
程序中没有检查输入字符串的最大长度是否超过结构中定义变量所能容纳的最大长度。

#define MAXTITLE 2 //标题
#define MAXAUTL 2 //作者
#define MAXBKS 2 //最多可以容纳图书册数

struct book //建立book模板
{
char title[MAXTITLE];
char author[MAXAUTL];
float value;
};

title最大可以容纳1个字符和1个'\0'串结束符。
author最大可以容纳1个字符和1个'\0'串结束符。
橡皮擦 2012-02-17
  • 打赏
  • 举报
回复
struct book //建立book模板
{
char title[MAXTITLE];
char author[MAXAUTL];
float value;
};

title 和 author 都是拥有两个char类型变量的数组


library[count].title 和
library[count].author 字符宽度都是2

所以只读入了两个字符

EmbeddedLong 2012-02-16
  • 打赏
  • 举报
回复
Enter the next title
222
Now enter the book author
eee


为什么title会多输出 但是author会少输出一个呢,大家可以试试其他输入 搞不懂,费解

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧