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

西北虎狼 西北虎狼 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我本来想在一个程序中定义两个结构体,分别定义姓名和个人信息,在主程序中部分初始化结构体数组,然后调用两个函数(输入信息和输出信息)获取三个人的完整信息,当然平均分先不去管。编出来后运行没问题,但就是始终个人信息中的分数不能正确获取,我把代码附在下面,请各位帮忙指点下!

//功能:通过调用函数的形式接受数据输入,并调用另外一个打印函数输出信息
//时间: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; //如果不是回车继续,将回车之前的字符都忽略
}
}
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
西北虎狼
C语言 70,039 成员 243,237 内容