结构体部分编程的一个困惑
我本来想在一个程序中定义两个结构体,分别定义姓名和个人信息,在主程序中部分初始化结构体数组,然后调用两个函数(输入信息和输出信息)获取三个人的完整信息,当然平均分先不去管。编出来后运行没问题,但就是始终个人信息中的分数不能正确获取,我把代码附在下面,请各位帮忙指点下!
//功能:通过调用函数的形式接受数据输入,并调用另外一个打印函数输出信息
//时间: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; //如果不是回车继续,将回车之前的字符都忽略
}
}
...全文
相关推荐
用python计算lda语言模型的困惑度并作图
用python计算lda语言模型的困惑度并作图
下面程序是定义一个结构体,并为其定义结构体变量,对其值输入、输出。
有五个学生,每个学生的数据包括学号,姓名,三门课的成绩
结构体使用详解,与结构体如何嵌套使用
详细介绍了,结构体的声明,初始化,与使用成员变量的各种方法,其中最大的亮点是结构体的嵌套使用,类和联合也是同样的道理,大家共同学习呀!!
c++里初始化结构体的一个比较的简单方法
//常用方法 struct MYSTRUCT ...{ int i,j,q; char szName[15]; MYSTRUCT() ...{ i = j = q = 0; memset(szName,0,sizeof(szName)); } }; //我想的方法 一个初始化结构体比较省事的方法
串口编程DCB结构体参数配置详细说明
串口编程DCB结构体参数配置详细说明,串口编程DCB结构体参数配置详细说明.


