简体   繁体   中英

C Programming BMI Program

Im having trouble using pointers and references with this program. I dont understand it entirely. Im still pretty new at C and we only touched on pointers but haven't gone over it that much. Any help will be appreciated.

EDIT: Now it's not letting me input anything...

Here's my new code:

#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);

}

Warning #1: Your getStats function can exit in two places, but only the first place actually returns a value. it should be some more like:

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

Warning #2: You declare bodyMassIndex at the start of the function, but then pass it into printWeightStatus without ever having assigned a value to it:

Warning #3: Ditto, you declare statsFP, but pass it into a function without every initializing it, and THEN initialize it within getStats

Your first error is that function getStats does not always return a value.

Indeed, when I look at the function, I don't see any return statement anywhere in the function.

When I look at the prototype, I see:

int getStats(...

indicating that it is supposed to return an int .

You should either change the function so that it returns an int , or change the declaration of the function to be void , indicating that it is not supposed to return a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM