结构体部分编程的一个困惑

西北虎狼 2012-02-28 10:08:03
我本来想在一个程序中定义两个结构体,分别定义姓名和个人信息,在主程序中部分初始化结构体数组,然后调用两个函数(输入信息和输出信息)获取三个人的完整信息,当然平均分先不去管。编出来后运行没问题,但就是始终个人信息中的分数不能正确获取,我把代码附在下面,请各位帮忙指点下!

//功能:通过调用函数的形式接受数据输入,并调用另外一个打印函数输出信息
//时间:2012.02.28
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define CSIZE 3
#define LEN 15
#define SCORE 3

struct Name
{
char m_sLasName[LEN];
char m_sFamName[LEN];
};

struct People
{
struct Name m_sName;
double m_fScore[SCORE];
double m_fAverage;
};

void InputInfor(struct People people[], int iCount); //接收输入
void PrintInfor(struct People people[], int iCount); //打印信息
void CleanLine(void);

int main(int argc, char * argv[])
{
struct People people[CSIZE] = { //部分初始化
{"Liang", "Bai"},
{"Chunxi", "Wang"},
{"Lei", "Bai"}
};

printf("**************Enter information for 3 people**************\n");
InputInfor(people, CSIZE);
printf("**************After receive information*******************\n");
PrintInfor(people, CSIZE);

return 0;
}

void InputInfor(struct People people[], int iCount)
{
int iOut, iInner;

for (iOut = 0; iOut < iCount; ++iOut)
{
printf("NO.%d\n", iOut + 1);
puts("Enter last name:");
gets(people[iOut].m_sName.m_sLasName);
puts("Enter family name:");
gets(people[iOut].m_sName.m_sFamName);

for (iInner = 0; iInner < SCORE; ++iInner)
{
printf("Enter %d score:\n", iInner + 1);
scanf("%f", &people[iOut].m_fScore[iInner]);
}

CleanLine(); //清除接收数据时的回车
}
}

void PrintInfor(struct People people[], int iCount)
{
int iVal;

for (iVal = 0; iVal < iCount; ++iVal)
{
printf("NAME |%10s|%10s\n", people[iVal].m_sName.m_sFamName,
people[iVal].m_sName.m_sLasName);
printf("SCORE |%10.2f|%10.2f|%10.2f\n", people[iVal].m_fScore[0],
people[iVal].m_fScore[1], people[iVal].m_fScore[2]);
printf("AVERAGE|%10.2f\n\n", people[iVal].m_fAverage);
}
}

void CleanLine(void)
{
while (getchar() != '\n')
{
continue; //如果不是回车继续,将回车之前的字符都忽略
}
}
...全文
64 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
西北虎狼 2012-02-29
  • 打赏
  • 举报
回复
谢谢这位仁兄的帮忙!问题解决了!看来细节决定一切这话没错!
blingpro 2012-02-28
  • 打赏
  • 举报
回复
for (iInner = 0; iInner < SCORE; ++iInner)
{
printf("Enter %d score:\n", iInner + 1);
scanf("%lf", &people[iOut].m_fScore[iInner]); //double 是 lf
}

69,371

社区成员

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

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