-1

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.

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;

}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Also take a look at using cin and [why would we call cin clear and cin ignore after reading input](http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input) – Richard Chambers Sep 27 '14 at 00:10

3 Answers3

1

You have a spurious semicolon on this line:

else (bmi >= 25); {
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
0

cin using whitespace as the default delimiter. So, when you enter a name like "First Last", cin >> name; will only consume the word "First", while cin >> height; will try to consume "Last" and you will encounter an error. If you want to allow full names, try using std::getline.

Pradhan
  • 16,391
  • 3
  • 44
  • 59
0

When you use >> to input a string you only get a word of input. The rest of a user's full name sits there in the input buffer till the next input operation, which might be e.g. input of an integer. And then that text is invalid as an integer specification.

Instead use getline to read a line of text.


In passing, in order to get warnings about e.g. pure expressions used as statements (which is usually erroneous, and there's an example of that in your code), up your compiler's warning level. With Visual C++ use /W4. With g++ use -Wall.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331