0

After my program writes to a text file, I am getting strange characters. For example:

Enter Weight in Pounds and Height in Inches: ÍÍÍÍYour BMI is: 25.11

Overweight

These "ÍÍÍÍ" should be numbers i.e. : 170 70.

Has anyone else come upon this problem? If you need to see my code here it is.

#include <stdio.h>
#include <stdlib.h>

FILE *fp;
void Calculate_BMI(double Weight_in_Pounds, double Hieght_in_Inches, double BMI);
int main()
{
    fp = fopen("csis.txt", "w");
    int i;
    double Wieght_in_Pounds, Hieght_in_Inches, BMI;

    Wieght_in_Pounds = 0;
    Hieght_in_Inches = 0;
    BMI = 0;
    for (i = 1; i <= 4; ++i) {
        Calculate_BMI(Wieght_in_Pounds, Hieght_in_Inches, BMI);
    }

    fclose(fp);
    system("pause");
    return(0);
}

void Calculate_BMI(double Weight_in_Pounds, double Hieght_in_Inches, double 
BMI)
{
    printf("Enter Weight in Pounds and Hieght in Inches: ");
    fprintf(fp, "Enter Weight in Pounds and Hieght in Inches: ");
    scanf(" %lf %lf", &Weight_in_Pounds, &Hieght_in_Inches);
    fscanf(fp, " %lf %lf", &Weight_in_Pounds, &Hieght_in_Inches);
    BMI = (Weight_in_Pounds * 703) / (Hieght_in_Inches * Hieght_in_Inches);
    printf("Your BMI is: %3.2lf\n", BMI);
    fprintf(fp, "Your BMI is: %3.2lf\n", BMI);
    if (BMI < 18.5) {
        printf("Under Weight\n");
        fprintf(fp, "Under Weight\n");
    }
    else if (BMI >= 18.5, BMI <= 25.0){
        printf("Normal weight\n");
        fprintf(fp, "Normal weight\n");
    }
    else if (BMI >= 25.0, BMI <= 30.0) {
        printf("Overweight\n");
        fprintf(fp, "Overweight\n");
    }
    else {
        printf("Obese\n");
        fprintf(fp, "Obese\n");
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Fyi, `if (BMI >= 18.5, BMI <= 25.0)` - that clearly doesn't do what you think it does. Read about [the comma operator](https://stackoverflow.com/questions/1737634/c-comma-operator). And turn up your compiler warnings and treat warnings as errors. – WhozCraig Jun 11 '17 at 02:52
  • Huh, okay thanks. It's weird though because it didn't give me any warnings at all. Though still comes out with the correct answers. – Anthony Davis Jun 11 '17 at 02:58
  • @Anthony. Yes you got lucky just coz of `else if`. if they were multiple `if` statements, output would be different –  Jun 11 '17 at 03:43
  • [Indentation...](https://en.wikipedia.org/wiki/Indent_style) – Antti Haapala -- Слава Україні Jun 11 '17 at 05:14
  • Note that passing arguments to the function that are then unconditionally overwritten by `scanf()` calls in the function is pointless. The function should take no arguments and the parameters should become local variables. You should note that 'weight' and 'height' both violate the 'i before e except after c' rule — and should be spelled consistently. You should check the return value from `scanf()` and `fscanf()`. There's no point in writing `scanf(" %lf %lf", &Weight_in_Pounds, &Hieght_in_Inches); fscanf(fp, " %lf %lf", &Weight_in_Pounds, &Hieght_in_Inches);` — you throw away user input. – Jonathan Leffler Jun 11 '17 at 05:49

1 Answers1

1
  1. Invalid operation fscanf() on a file opened in write mode (w). Remove the fscanf() statement.

  2. replace , with && operator in if statements. The result of comma(,) operator has the same type and value of its right most operand in the sequence. so only the results of BMI <= 25.0 and BMI <= 30.0 are considered.

    if (BMI >= 25.0 && BMI <= 30.0)
    
  3. Also don't forget to check the return value of fopen() and scanf().

  • I get that it is an invalid operation. But I still wanna take in the user input with out those weird characters showing up. How do I do that since all I know is fscanf – Anthony Davis Jun 11 '17 at 21:58
  • you'll have to compare the return value of `scanf()` against 2. return from the function if it happens otherwise the old/garbage values might be taken as inputs –  Jun 12 '17 at 04:17