[ create a new paste ] login | about

Link: http://codepad.org/Yn2ulRxd    [ raw code | output | fork ]

C++, pasted on Jul 10:
#include <iostream>
using namespace std;

// the main function has 2 parameters!
// some compilers might complain if you omit them
int main(int, char**)
{
  cout << "weight (in KG): ";
  double weight;
  cin >> weight;

  cout << "height (in meters): ";
  double height;
  cin >> height;

  // since you compare a double to a constant, i'd take a double
  // constant (0.0 instead of 0), but that's almost purely cosmetic
  if ((weight <= 0.0) || (height <= 0.0))
  {
    // you can use \n instead of endl, saves you some <<
    cout << "invalid input\n";
  }
  else
  {
    double bmi = (weight / (height * height));
    cout << "Your Body Mass Index is " << bmi << ".\n";

    // check your code with bmi 20.0 ;)
    if ((bmi > 0.0) && (bmi <= 20.0))
    {
      cout << "You are within the healthy BMI range. Congratulations!\n";
    }
    else if ((bmi > 20.0) && (bmi <= 25.0))
    {
      cout << "You are within the healthy BMI range. Congratulations!\n";
    }
    else
    {
      cout << "You have exceeded the healthy BMI range.\n";
    }
  }

  return 0;
}


Output:
1
2
weight (in KG): height (in meters): Your Body Mass Index is 2.71896e+223.
You have exceeded the healthy BMI range.


Create a new paste based on this one


Comments: