问题遇到的现象和发生背景
遇到的现象和发生背景,请写出第一个错误信息
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define N 3
typedef struct
{
unsigned int day; // should be checked against 28/29/30/31
unsigned int month; // considering using an enum type:January/February/...
unsigned int year; // should be given a reasonable range
} DATE;
typedef struct
{
char ID[20]; // avoid using as an interger
char name[20];
char gender; // considering using an enum type:M/F/O
DATE birthday;
float height;
float weight;
float BMI;
char BMI_grade[20];
} STAFF;
//BMI grading strings
const char* BMI_grade[5] = {
"under Weight",
"Normal",
"Over Weight",
"Obese",
"Extremely Obese" };
int main(int argc, char* argv[])
{
// function declarations
int input(STAFF * p);
int process(STAFF * p);
int sort(STAFF * p);
int output(STAFF * p);
int save(char* filename, STAFF * p);
//array definitions
STAFF staff[N];
char filename[20];
// parameter processing
if (argc != 2)
{
printf("Too many or few parameters provided!\n");
printf("Usage: exp01.exe HealthCheck.txt\n");
exit(0);
}
else
{
strcpy(filename, argv[1]);
}
// function calls
input(staff);
process(staff);
sort(staff);
output(staff);
save(filename, staff);
return 0;
}
int input(STAFF* p)
{
printf("Stasff data inputting...\n");
for (int i = 0; i < N; i++, p++)
{
setbuf(stdin, NULL); // to empty the keyboard file buffer
printf("\nNow Please input the %d(th) person's data\n", i);
printf(" ID: ");
gets_s(p->ID);
printf(" Name: ");
gets_s(p->name);
printf(" Gender(F/M): ");
p->gender = getchar();
while (p->gender != 'm' && p->gender != 'M' && p->gender != 'f' && p->gender != 'F')
{
printf(" Invalid gender input! Retry as (f/F/m/M): ");
getchar();
p->gender = getchar();
}
printf(" Birthday(year month day): ");
scanf("%d%d%d",
&(p->birthday.year),
&(p->birthday.month),
&(p->birthday.day));
// The following codes can be further tuned to chenck the day element in a month-wise way
while (p->birthday.year > 2000 ||
p->birthday.year < 1950 ||
p->birthday.month < 1 ||
p->birthday.month > 12 ||
p->birthday.day < 0 ||
p->birthday.day > 31)
{
printf(" Invalid month/day input! Retry Please : ");
scanf("%d%d%d",
&(p->birthday.year),
&(p->birthday.month),
&(p->birthday.day),50,50,50);
}
printf(" Height(cm): ");
scanf_s("%f", &(p->height));
printf(" Weight(kg): ");
scanf_s("%f", &(p->weight));
}
printf("Stasff data inputting DONE\n");
return 0;
}
int process(STAFF* p)
{
printf("Stasff data processing...");
for (int i = 0; i < N; i++, p++)
{
p->BMI = p->weight / p->height / p->height * 10000.0;
if (p->BMI < 18.5)
strcpy(p->BMI_grade, BMI_grade[0]);
else if (p->BMI < 25.0)
strcpy(p->BMI_grade, BMI_grade[1]);
else if (p->BMI < 28.0)
strcpy(p->BMI_grade, BMI_grade[2]);
else if (p->BMI < 32.0)
strcpy(p->BMI_grade, BMI_grade[3]);
else
strcpy(p->BMI_grade, BMI_grade[4]);
}
printf(" DONE\n");
return 0;
}
int sort(STAFF* p)
{
int i, j, k;
STAFF temp;
printf("Stasff data sorting...");
for (i = 0; i < N - 1; i++)
{
k = i;
for (j = i + 1; j < N; j++)
if (p[j].BMI < p[k].BMI)
k = j;
if (k != i)
{
temp = p[i];
p[i] = p[k];
p[k] = temp;
}
}
printf(" DONE\n");
return 0;
}
int output(STAFF* p)
{
printf("\nID\tName\tGender\tBirthday\tHeight(cm)\tWeight(kg)\tBMI\tBMI_Grade\n");
for (int i = 0; i < N; i++, p++)
{
printf("%s\t%s\t%c\t%d-%d-%d\t%.2f\t%.2f\t%.2f\t%s\n",
p->ID,
p->name,
p->gender,
p->birthday.year,
p->birthday.month,
p->birthday.day,
p->height,
p->weight,
p->BMI,
p->BMI_grade
);
}
return 0;
}
int save(char* filename, STAFF* p)
{
FILE* fp;
if ((fp = fopen(filename, "w")) == NULL)
{
printf("File open error.\n");
exit(0);
}
printf("Stasff data saving...");
fprintf(fp, "ID\tName\tGender\tBirthday\tHeight(cm)\tWeight(kg)\tBMI\tBMI_Grade\n");
for (int i = 0; i < N; i++, p++)
{
fprintf(fp, "%s\t%s\t%c\t%d-%d-%d\t%.2f\t%.2f\t%.2f\t%s\n",
p->ID,
p->name,
p->gender,
p->birthday.year,
p->birthday.month,
p->birthday.day,
p->height,
p->weight,
p->BMI,
p->BMI_grade
);
}
printf(" DONE\n");
fclose(fp);
return 0;
}
运行结果及详细报错内容
打开调试一会就自动关掉了,scanf显示返回值被忽略