-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmi.cc
54 lines (45 loc) · 1.24 KB
/
bmi.cc
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
#include <cassert>
#include <cstdlib>
#include <iostream>
using namespace std;
constexpr double inches_per_meter = 39.37;
constexpr double lbs_per_kg = 2.205;
const string category[3]{
"normal", "overweight", "obese",
};
double inches_to_m(double length) {
assert(length > 0.0);
return (length / inches_per_meter);
}
double lbs_to_kg(double weight) {
assert(weight > 0.0);
return (weight / lbs_per_kg);
}
double calc_bmi(double height, double weight) {
assert(height > 0);
return (weight / (height * height));
}
int main(int argc, char **argv) {
double height, weight;
string name, type;
if (argc != 4) {
cout << "Enter name, height, weight." << endl;
exit(EXIT_FAILURE);
}
name = argv[1];
if ((sscanf(argv[2], "%lf", &height) != 1) ||
(sscanf(argv[3], "%lf", &weight) != 1)) {
cout << "Enter name, height (inches), weight (lbs)." << endl;
exit(EXIT_FAILURE);
}
double bmi = calc_bmi(inches_to_m(height), lbs_to_kg(weight));
if (bmi <= 25)
type = category[0];
else if (bmi >= 40)
type = category[2];
else
type = category[1];
cout << "Name: " << name << " Weight: " << weight << " Height: " << height
<< " BMI: " << bmi << " Category: " << type << endl;
exit(EXIT_SUCCESS);
}