Search code examples
c++mathcalculatorsubtraction

Subtraction is giving me positive result C++


if (GoalWeight < 0.0) {
    int weeks;
    cout << "How many weeks do you plan to continue this trend?\n";
    cin >> weeks;
    double NewWeight = GoalWeight * weeks;
    double NegBMI = (weight - NewWeight) * 703 / (pow(HeightConverter, 2));
    cout << "If you complete your plan for " << weeks << "weeks you will have a new BMI of: \n" << NegBMI;
}
system("pause");
return 0;

}

Output result:

What is your current weight?: 180

What is your current height in inches?" 71

Your current BMI is: 25.10(Not part of output, but this is correct)

What is your goal weight change?(lbs) -1.5

How many weeks do you plan to continue this trend?: 6

If you complete your plan for 6 weeks you will have a new BMI of: 26.36

As you can tell this is wrong

The calculation for BMI is (weight * 703) /height^2(inches)

What it is doing for negative numbers is:

180 + 9(instead of 180 - 9) giving (191 * 703) / 71^2 yielding 26.36

Instead of:

180 - 9(giving 171 * 703) / 71^2 yielding the correct output of:23.84

I know you're all shaking your heads saying I must be an idiot, and rightfully so, I'm hoping someone can help me with this!


Solution

  • What is your goal weight change?(lbs) -1.5

    How many weeks do you plan to continue this trend?: 6

    6 * ( -1.5 ) == -9
    180 - (-9) == 189
    

    So you either input goal weight change as positive number or add it, not subtract.