BMI Calculator

I'm a beginner, so take it easy on me.
My problem is in my output. If you put in a full name, it crashes. Should I have used something other than string? It correctly displays the output initially, but then it also runs down, and adds the you are overweight line, and an additional computation. It works perfectly if the computation adds up to the user being overweight. Also, the set precision isn't being applied. I'm stumped.

#include <iostream> // This is for user input.
#include <cmath> // This is to perform the "pow" function.
#include <iomanip> // This is to format the output.
# include <string> // This is to store the name, or the whole name of the user if inputted.
using namespace std;

int main()

{
double height, weight, bmi; // height in inches, weight in pounds, bmi is total bmi of user
string name; // name of the user
int num; // the number 703 used for bmi calculation
num = 703; // constant used for bmi calculation

cout << "Please enter your full Name:"; // Asking the user to input their name
cin >> name; // Users name

cout << "Please enter your height(in inches):"; // User height in inches
cin >> height; // Users height

cout << "Please enter your weight(in lbs):"; //Users weight in lbs
cin >> weight; // Users weight

bmi = (weight / pow(height, 2)) * num; // the actual calculation of the bmi of the user

if (bmi >= 18.5 && bmi < 25) {
cout << name << " your BMI is " << setprecision(1) << bmi; // outputting to the user their actaul BMI
cout << endl;
cout << "You are in the optimal weight category!"; // outputting their category
}
else if (bmi < 18.5) {
cout << name << " your BMI is " << setprecision(1) << bmi;
cout << endl;
cout << "You are underweight.";
}
else (bmi >= 25); {
cout << name << " your BMI is " << setprecision(1) << bmi;
cout << endl;
cout << "You are overweight.";
}
return 0;
}
Last edited on
Hi,

The else cannot have a condition. Make it another else if, then use the else to catch any errors - things that don't match any other condition.

Please use code tags - select your code then press the <> button on the format menu.

:+)
Topic archived. No new replies allowed.