### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114`` ``````#include #include 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 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.