编写了genderstat()函数用于实现按性别统计学生信息,该函数可输出男性或女性的学生信息并输出男生/女生人数。但运行时编译器报错,这个该如何解决呢?
报错如下:
void genderstat() //该函数用于实现按性别统计
{
int i,count=0; //count变量用于计数
char gender,c;
FILE * fp; //定义一个文件指针fp
read(); //读取students文本文件
printf("请输入要统计的性别(m/f):\n");
scanf("%c",&gender);
do{
if(gender == 'm')
{
printf("|#学号\t\t姓名\t性别\t年龄\t数学分析\t高等代数\t程序设计\t大学英语\tGPA\t#|\n");
for(i = 0;i < n; i++){
if(strcmp(st[i].gender,'m') == 0) //比较字符串
{
count++;
printf(" %d ", st[i].id);
printf(" %-s ", st[i].name);
printf(" %-s ", st[i].gender);
printf(" %d ", st[i].age);
printf("\t%d ", st[i].MathAna);
printf("\t\t%d ", st[i].LiAlg);
printf("\t\t%d ", st[i].Computer);
printf("\t\t%d ", st[i].English);
printf("\t\t%.2f \n", st[i].GPA);
}
}
printf("\n\t\t共%d名男生",count);
}
else
{
printf("|#学号\t\t姓名\t性别\t年龄\t数学分析\t高等代数\t程序设计\t大学英语\tGPA\t#|\n");
for(i = 0;i < n; i++){
if(strcmp(st[i].gender,'f') == 0) //比较字符串
{
count++;
printf(" %d ", st[i].id);
printf(" %-s ", st[i].name);
printf(" %-s ", st[i].gender);
printf(" %d ", st[i].age);
printf("\t%d ", st[i].MathAna);
printf("\t\t%d ", st[i].LiAlg);
printf("\t\t%d ", st[i].Computer);
printf("\t\t%d ", st[i].English);
printf("\t\t%.2f \n", st[i].GPA);
}
}
printf("\n\t\t共%d名女生",count);
}
printf("\n\n\t\t\t\t是否继续该操作(Y/N):"); // 提示是否继续
scanf("%c", &c);
c = getchar();
}while(c == 'Y'||c == 'y');
}
strcmp第二个参数,字符串要用双引号
strcmp是用来比较两个字符串的,你后面不要只写一个字符啊
如果是字符比较,直接==就好
修改如下,供参考:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int n = 0;
struct Student { //设置结构体变量,储存学生信息
int id; //学号
char name[10]; //姓名
char gender[10]; //性别
int age; //年龄
int MathAna; //数学分析成绩
int LiAlg; //高等代数成绩
int Computer; //程序设计成绩
int English; //大学英语成绩
float GPA; //平均学分绩GPA
};
struct Student st[100];
void genderstat() //该函数用于实现按性别统计
{
int i, count = 0; //count变量用于计数
char gender, c;
FILE* fp; //定义一个文件指针fp
read(); //读取students文本文件
do { //修改
printf("请输入要统计的性别(m/f):\n"); //修改
scanf(" %c", &gender); //修改
//scanf("%c", &gender);
count = 0; //修改
if (gender == 'm')
{
printf("|#学号\t\t姓名\t性别\t年龄\t数学分析\t高等代数\t程序设计\t大学英语\tGPA\t#|\n");
for (i = 0; i < n; i++) {
if (strcmp(st[i].gender, "m") == 0) //比较字符串
//strcmp(st[i].gender, 'm') == 0 //修改
{
count++;
printf(" %d ", st[i].id);
printf(" %-s ", st[i].name);
printf(" %-s ", st[i].gender);
printf(" %d ", st[i].age);
printf("\t%d ", st[i].MathAna);
printf("\t\t%d ", st[i].LiAlg);
printf("\t\t%d ", st[i].Computer);
printf("\t\t%d ", st[i].English);
printf("\t\t%.2f \n", st[i].GPA);
}
}
printf("\n\t\t共%d名男生", count);
}
else if(gender == 'f') //修改
{
printf("|#学号\t\t姓名\t性别\t年龄\t数学分析\t高等代数\t程序设计\t大学英语\tGPA\t#|\n");
for (i = 0; i < n; i++) {
if (strcmp(st[i].gender, "f") == 0) //比较字符串
//strcmp(st[i].gender, 'f') == 0 //修改
{
count++;
printf(" %d ", st[i].id);
printf(" %-s ", st[i].name);
printf(" %-s ", st[i].gender);
printf(" %d ", st[i].age);
printf("\t%d ", st[i].MathAna);
printf("\t\t%d ", st[i].LiAlg);
printf("\t\t%d ", st[i].Computer);
printf("\t\t%d ", st[i].English);
printf("\t\t%.2f \n", st[i].GPA);
}
}
printf("\n\t\t共%d名女生", count);
}
printf("\n\n\t\t\t\t是否继续该操作(Y/N):"); // 提示是否继续
scanf(" %c", &c); //修改
//c = getchar(); //修改
} while (c == 'Y' || c == 'y');
}