我在使用该程序的指针和引用时遇到问题。我完全不明白。我还是 C 的新手,我们只涉及指针,但还没有过多地讨论它。任何帮助将不胜感激。
编辑:现在它不让我输入任何东西......
这是我的新代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define F 703
int getStats(FILE *statsfp, int *patientID, double *weight, double *height, double *bodymassIndex);
double getBodyMassIndex(double weight, double height);
void printWeightStatus(FILE *statsfp, int patientID, double weight, double height, double bodyMassIndex);
void pause()
{
char ans;
fflush(stdin);
printf("\nPress return to continue");
scanf("%c", &ans);
}
int main() {
FILE statsfp;
int patientID;
double weight, height, bodyMassIndex;
getStats(&statsfp,&patientID, &weight, &height, &bodyMassIndex);
pause();
return 0;
}
int getStats(FILE *statsfp, int *patientID, double *weight, double *height, double *bodyMassIndex)
{
statsfp = fopen("patientStats.txt","r");
if (statsfp == NULL)
{
printf("\nFailed to open the %s file.\n", "patientStats.txt");
pause();
exit(1);
}
printf("\nPatient ID\t Weight\t Height\t BMI\t Weight Status\n");
printf("\n---------------------------------------------------\n");
while (fscanf (statsfp, "%d %lf %d", &patientID, &weight, &height) !=EOF)
{
getBodyMassIndex(*weight, *height);
printWeightStatus(statsfp, *patientID, *weight, *height, *bodyMassIndex);
}
fclose(statsfp);
return 0;
}
double getBodyMassIndex(double weight, double height)
{
double bodyMassIndex = 0;
bodyMassIndex = (F*weight)/(height * height);
return bodyMassIndex;
}
void printWeightStatus(FILE *statsfp, int patientID, double weight, double height, double bodyMassIndex)
{
char *weightStats;
if (bodyMassIndex < 18.5)
weightStats = "underweight";
else if (bodyMassIndex >= 18.5)
weightStats = "normal";
else if (bodyMassIndex >= 25.0)
weightStats = "overweight";
else if (bodyMassIndex >= 30.0)
weightStats = "obese";
printf("%6d\t %6.2f\t %6.2f\t %s", &patientID,&weight, &height, weightStats);
}