BMI Calculator Project (weight formulas)

Wasnt sure if this should go in the beginners section or not, but here is my issue. I have to program a BMI calculator that calculates the users BMI and tells them if they are underweight, overweight, obease, or normal and also tells them how much weight they need to lose or gain to get back to a "normal" bmi. I got most of the program figured out but my main problem is calculating how much weight they need to gain/lose to get back to "normal."

tl;dr I need help with weight gain/loss formulas to get the user back to a normal bmi.

Included is sauce 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <windows.h>
using namespace std;
 
int main()
{
	HANDLE hConsole = GetStdHandle (STD_OUTPUT_HANDLE);

	const int	bmi_const=703;
 
	double		bmi,
				weight,
				height,
				under,
				abs_under,
				abs_over,
				over;

	char		again='n';

	SetConsoleTextAttribute
	(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);

	do
	{
		cout << "Please enter your weight in pounds: ";
 
		while ( (!(cin >> weight)) | (weight <= 0)) 
		{ 
			cout << "ERROR - Invaild entry. Please reenter your weight in pounds: ";
			cin.clear();
			fflush(stdin);
		}

		cout << "\nPlease enter your height in inches: ";
 
		while ( (!(cin >> height)) | (height <= 0))
		{
			cout << "ERROR - Invaild entry. Please reenter your height in inches: ";
			cin.clear();
			fflush(stdin);
		}

		cout << "\nYou weigh " << weight << " pounds, and you are " << height << " inches tall." << endl;
 
		bmi = (weight * bmi_const) / (height * height);
		under = ((bmi - 25) / ((height * height) / bmi_const));
		abs_under = abs(under);
		over = ((18.5 - bmi) * ((height * height)/bmi_const));
		abs_over = abs(over);

		cout << "Your BMI is: " << bmi << endl;
 
		if (bmi < 18.5)
		{
			cout << "\nYou are ";
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
			cout << "UNDERWEIGHT";
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
			cout << "." << endl; 
			cout << "You need to gain " << abs_under << " pounds.";
		}
		else if ((bmi >= 18.5) && (bmi <= 25)) 
		{
			cout << "\nYou are ";
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
			cout << "NORMAL";
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
			cout << ".  Go live a happy life." << endl; 
		}
		else if ((bmi > 25) && (bmi <= 30)) 
		{
			cout << "\nYou are ";
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
			cout << "OVERWEIGHT";
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
			cout << "." << endl;  
			cout << "You need to lose " << abs_over << " pounds." << endl;
		}
		else if (bmi > 30)
		{
				cout << "\nYou are ";
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
			cout << "OBEASE";
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
			cout << "." << endl; 
			cout << "You need to lose " << abs_over << " pounds." << endl;
		}	
				

		cout << "\n\n\n\n\t\tThis was programmed by: ";
		SetConsoleTextAttribute
			(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
		cout << "Devon Naccarato";
		SetConsoleTextAttribute
			(hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
		cout << "." << endl;


		cout << "\n\n\nWould you like to calculate someone else's BMI? [Y/N]: ";
		cin >> again;

	} while(again =='Y' || again =='y');

	return 0;
}
The only thing that I could find is that you need to add #include <cmath> because on lines 48 and 50, you use the abs() function. Otherwise everything else looks good. I compiled and ran the code, I just didn't validate that the math was correct.
Last edited on
The only thing that I could find is that you need to add #include <cmath> because on lines 48 and 50, you use the abs() function. Otherwise everything else looks good. I compiled and ran the code, I just didn't validate that the math was correct.


Thanks for that tip. I mainly needed help with the weight calculation formulas, but I figured it out.
No problem. It looked like the equations were functioning properly from the start. But, as I said, I didn't validate the output. I'm glad you got it running the way you wanted though!
Topic archived. No new replies allowed.