gpt4 book ai didi

C 编程 BMI 程序

转载 作者:行者123 更新时间:2023-11-30 15:44:23 25 4
gpt4 key购买 nike

我在该程序中使用指针和引用时遇到问题。我完全不明白。我对 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);

}

最佳答案

警告 #1:您的 getStats 函数可以在两个位置退出,但只有第一个位置实际返回值。它应该更像是:

function getStats() {
if (...) {
return foo;
}
....
return baz; <--missing this
}

警告 #2:您在函数开头声明 bodyMassIndex,但随后将其传递给 printWeightStatus,而没有为其赋值:

警告 #3:同上,您声明 statsFP,但将其传递到函数中而不每次都对其进行初始化,然后在 getStats 中对其进行初始化

关于C 编程 BMI 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19504062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com