BMI calculator help

I made a BMI calc in C++, and decided I'd want to add a portion that calculates a users new BMI given a weight change weekly(say, losing 2 pounds every week, or gaining 2 pounds every week, for n weeks) I have two if statements set up, so that when asked what weight change they were looking for, based on that result it would jump to one if statement or the other. What I mean, is that if the user wants to LOSE weight, they would put -2, which would jump to the if statement I have setup for losing weight. If a user wanted to GAIN weight, it would jump to another if statement, set up to calculate the new BMI given the new weight change. Here are my two if statements setup:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stdafx.h"
#include <iostream> // Allows us to use cout and cin
#include <cmath> // To allow us to calculate the BMI
#include <iomanip> // Set specific decimal points(two)
using namespace std;

int main()
{
double weight;
cout << "What is your current weight?: "; // Using the cout() function, we get user input
cin >> weight;
double heightFeet;
cout << "What is your height(in feet, just the first part(Example, if you're 5'11, put 5)";
cin >> heightFeet;
double heightInches;
cout << "What is your height in inches, just the second part(Example if you're 5'11, put 11)\n";
cin >> heightInches;
double HeightConverter = 12 * heightFeet + heightInches;
cout << "Your height in inches is: " << HeightConverter << "\n";
double BMICalc = (weight * 703) / (pow(HeightConverter, 2));
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << "Your current BMI is: " << BMICalc << "\n";
double GoalWeight;
std::cout << "What is your goal weight change per week? (lb)\n";
cin >> GoalWeight;
if (GoalWeight >= 0) {
    int weeks;
    cout << "How many weeks do you plan to continue this trend?\n";
    cin >> weeks;
    double PosBMI = (weight + GoalWeight) * 703 / (pow(HeightConverter, 2));
    cin >> PosBMI;
    cout << "If you complete your plan for " << weeks << "weeks you will have a new BMI of: \n" << PosBMI;

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

}

When this is compiled, it gets to "How many weeks do you plan to continue this trend?", if I put in 3, it pauses at 3 in the terminal, and does nothing, and eventually I have to just close it. Does anyone see what the issue is in the if statements? Sorry for the ugly code as well in advance. Bear with me here.
Last edited on
1
2
double NegBMI = (weight - GoalWeight) * 703 / (pow(HeightConverter, 2));
cin >> NegBMI;


Should be :
1
2
double NegBMI = (weight - GoalWeight) * 703 / (pow(HeightConverter, 2));
cin >> NegBMI;

Hi, thank you for the response, I removed cin >> NegBMI and cin >> PosBMI, when I do the math, I get the wrong result. Here's an example:


What is your current weight?: 180
What is your height(in feet, just the first part(Example, if you're 5'11, put 5)
5
What is your height in inches, just the second part(Example if you're 5'11, put
11)
11
Your height in inches is: 71
Your current BMI is: 25.10
What is your goal weight change per week? (lb)
-1.5
How many weeks do you plan to continue this trend?
6
If you complete your plan for 6weeks you will have a new BMI of:
25.31Press any key to continue . . .



The answer should be: 23.85
How is 'new BMI' calculated?

Your current BMI is: 25.10

Is it correct?
I fixed it, the positive portion works, but if I do "-1.5" it automatically adds, I assume because it's subtracting two negative numbers, automatically having it do addition(since two negatives make a positive), can you help me with the negative numbers(so -1.5 isn't seen as 1.5)?


Here's updated code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	double GoalWeight;
	std::cout << "What is your goal weight change per week? (lb)\n";
	cin >> GoalWeight;
	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 PosBMI = (weight + NewWeight) * 703 / (pow(HeightConverter, 2));
		cout << "If you complete your plan for " << weeks << "weeks you will have a new BMI of: \n" << PosBMI;

	}
	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;
	
}
Last edited on
You don't need the if statements.
1
2
3
4
5
6
7
8
9
    double GoalWeight;
    std::cout << "What is your goal weight change per week? (lb)\n";
    cin >> GoalWeight;
    int weeks;
    cout << "How many weeks do you plan to continue this trend?\n";
    cin >> weeks;
    double totalWeightChange = GoalWeight * weeks;
    double newBMI = (weight + totalWeightChange) * 703 / pow(HeightConverter, 2);
    cout << "If you complete your plan for " << weeks << "weeks you will have a new BMI of: \n" << newBMI;
Last edited on
Topic archived. No new replies allowed.