Xor
Xor

Reputation: 41

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!

Upvotes: 0

Views: 2182

Answers (3)

neumde
neumde

Reputation: 1

Do you believe that if you do (+NewWeight) the value of NewWeight becomes positive?

this is not the case:

Unary Plus Operator (+): The result of an operation on a numeric type is the value of the operand itself. This operator has been predefined for all numeric types.

As a solution use Reginalds idea and make (weight + newWeight) rather than the -.

Upvotes: 0

Reginald
Reginald

Reputation: 113

Your newWeight is resulting to -9 because of your statement 6 * -1.5.If you want to subtract it just make the (weight + newWeight) rather than the -.

Upvotes: 2

Slava
Slava

Reputation: 44238

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.

Upvotes: 2

Related Questions