-
Notifications
You must be signed in to change notification settings - Fork 10
/
bmi.cpp
72 lines (62 loc) · 1.45 KB
/
bmi.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// KJN - it is good practice to insert comment about intended use, context, contributors, etc
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int ounces2pounds(int x)
{
return(x*16);
}
int stones2pounds(int x)
{
return(x*14);
}
double weight2kg(int stones, int pounds, int ounces)
{
return (stones2pounds(stones)+pounds+ounces2pounds(ounces))/2.2;
}
double height2metres(int feet, int inches)
{
return(feet/3.82);
}
char categorise(double kg, double metre)
{
double bmi = kg*kg/metre;
char cat;
if (bmi<19)
cat='A';
else if (bmi<=26)
cat='B';
else if (bmi<=30)
cat='C';
else
cat='D';
return(cat);
}
void process_data(char* input_file, char* output_file)
{
ifstream f_in;
ofstream f_out;
string data;
string person_id;
int pounds, stones, ounces, feet, inches;
double kg, m;
char cat;
f_in.open(input_file,ios::in);
f_out.open(output_file,ofstream::out);
while (!f_in.eof())
{
f_in >> person_id >> pounds >> stones >> ounces >> feet >> inches;
kg=weight2kg(int(stones),int(pounds),int(ounces));
m =height2metres(int(feet),int(inches));
cat=categorise(kg,m);
f_out << person_id << " " << cat << endl;
}
f_in.close();
f_out.close();
}
int main(int argc, char *argv[])
{
// KJN - Need to check that 3 arguments were supplied upon execution
process_data(argv[1], argv[2]);
}