| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 960 人关注过本帖
标题:代码输出错误
取消只看楼主 加入收藏
captain2050
Rank: 2
等 级:论坛游民
帖 子:57
专家分:43
注 册:2016-7-15
结帖率:92.86%
收藏
已结贴  问题点数:20 回复次数:0 
代码输出错误
题目要求读入任意天数的浮点数温度,每天6个。
温度记录在动态分配内存的数组中,数组大小刚好等于输入的温度数。计算每天平均温度并输出,然后输出每天的温度记录。
程序的输出有问题。。。(setbuf(stdin,NULL);作用是清空输入缓冲区,linux上用的)

程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main()
{
    int day_num=1;
    float **p=malloc(day_num*sizeof(float*));
    char con;
   

    while(1)
    {
        p[day_num]=malloc(6*sizeof(float));
        setbuf(stdin,NULL);
        for(int i=0;i<6;i++)
            scanf("%f",(p[day_num]+i));
        printf("continue?(y or n)");
        setbuf(stdin,NULL);
        scanf("%c",&con);
        con=tolower(con);
        if(con=='n')
            break;
        else if(con=='y')
        {
            day_num++;
            p=realloc(p,day_num*sizeof(float*));
        }
        else
        {
            printf("input error\n");
            return 0;
        }
    }
   

    for(int i=0;i<day_num;i++)
        printf("%d:%f-%f,%f,%f,%f,%f,%f\n",i+1,(p[i][0]+p[i][1]+p[i][2]+p[i][3]+p[i][4]+p[i][5])/6,p[i][0],p[i][1],p[i][2],p[i][3],p[i][4],p[i][5]);
   

}



[此贴子已经被作者于2016-8-25 09:48编辑过]

搜索更多相关主题的帖子: 动态 缓冲区 linux 记录 
2016-08-25 09:45
快速回复:代码输出错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025408 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved