C语言菜鸟问题,给出答案,马上结分

meetyu 2009-02-19 09:55:46
//输入年龄,求平均年龄
#include <stdio.h>

main()
{
char an = 'y';
int ave = 0, sum = 0, num = 0;
int age;
do {
printf("Enter the number:\n");
scanf("%d",&age,++num);
sum += age;
printf("Do you wish to enter another num?\n");
scanf("%c",an);//为什么这名不起作用
}while(an == 'y');
ave = sum / num;
printf("%d members' average age is %d\n",num,ave);

}
...全文
74 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
meetyu 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 waizqfor 的回复:]
C/C++ code
#include <stdio.h>

void main()
{
char an = 'y';
int ave = 0, sum = 0, num = 0;
int age;
do {
printf("Enter the number:\n");
scanf("%d",&age,++num);
fflush(stdin);//第一输入完 要清空下 缓冲区
sum += age;
printf("Do you wish to enter another num?\n");
scanf("%c",&an);
}while(an == 'y');
ave = sum / num;

[/Quote]
正解.谢谢
waizqfor 2009-02-19
  • 打赏
  • 举报
回复

#include <stdio.h>

void main()
{
char an = 'y';
int ave = 0, sum = 0, num = 0;
int age;
do {
printf("Enter the number:\n");
scanf("%d",&age,++num);
fflush(stdin);//第一输入完 要清空下 缓冲区
sum += age;
printf("Do you wish to enter another num?\n");
scanf("%c",&an);
}while(an == 'y');
ave = sum / num;
printf("%d members' average age is %d\n",num,ave);

}
meetyu 2009-02-19
  • 打赏
  • 举报
回复
TC和VC6都试过了,都不行,得不到想要的结果
meetyu 2009-02-19
  • 打赏
  • 举报
回复
#include <stdio.h>

main()
{
char an = 'y';
int ave = 0, sum = 0, num = 0;
int age;
do {
printf("Enter the number:\n");
scanf("%d",&age,++num);
sum += age;
printf("Do you wish to enter another num?\n");
scanf("%c",&an);//改了,还是不行,为什么?
}while(an == 'y');
ave = sum / num;
printf("%d members' average age is %d\n",num,ave);

}
  • 打赏
  • 举报
回复
scanf("%c",&an);
和printf不一样,scanf是接收指针的。
lingyin55 2009-02-19
  • 打赏
  • 举报
回复
同上
拜倫 2009-02-19
  • 打赏
  • 举报
回复
同上
wshcy512 2009-02-19
  • 打赏
  • 举报
回复
同意楼上
hhllxxss 2009-02-19
  • 打赏
  • 举报
回复
应该是用scanf("%c",&an);吧

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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