62 return static_cast<int>(weight * 10000 / (height * height) + 0.5);
68 std::cin.ignore(std::numeric_limits<int>::max(),
'\n');
80 std::vector<int>& heights,
81 std::vector<int>& weights,
82 std::vector<char>& sexes)
89 std::cout <<
"Name " << names.size()+1 <<
": ";
90 if (not std::getline(std::cin, name))
95 int const min_height{10};
96 int const max_height{300};
97 std::cout <<
"Height (cm): ";
98 if (not (std::cin >> height))
101 if (height < min_height or height > max_height)
103 std::cout <<
"Invalid height. Aborting.\n";
109 int const min_weight{1};
110 int const max_weight{500};
111 std::cout <<
"Weight (kg): ";
112 if (not (std::cin >> weight))
115 if (weight < min_weight or weight > max_weight)
117 std::cout <<
"Invalid weight. Aborting.\n";
121 std::cout <<
"Sex (M or F): ";
122 if (not (std::cin >> sex))
125 sex = std::toupper(sex, std::locale());
126 if (sex !=
'M' and sex !=
'F')
128 std::cout <<
"Invalid sex. Aborting.\n";
134 names.push_back(name);
135 heights.push_back(height);
136 weights.push_back(weight);
137 sexes.push_back(sex);
156 std::vector<int>
const& heights,
157 std::vector<int>
const& weights,
158 std::vector<int>
const& bmis,
159 std::vector<char>
const& sexes,
160 std::vector<std::string>
const& names,
163 std::cout <<
"Ht(cm) Wt(kg) Sex BMI Name\n";
166 long int bmi_count(0);
167 std::vector<int> tmpbmis;
169 for (std::vector<int>::size_type i(0); i != heights.size(); ++i)
170 if (sexes.at(i) == sex)
172 bmi_sum = bmi_sum + bmis.at(i);
174 tmpbmis.push_back(bmis.at(i));
175 std::cout << std::setw(6) << heights.at(i)
176 << std::setw(7) << weights.at(i)
177 << std::setw(3) << sexes.at(i)
178 << std::setw(6) << bmis.at(i);
179 if (bmis.at(i) >= threshold)
183 std::cout <<
' ' << names.at(i) <<
'\n';
189 std::cout <<
"Mean BMI = "
190 << std::setprecision(1) << std::fixed << bmi_sum / bmi_count
195 std::sort(tmpbmis.begin(), tmpbmis.end());
196 std::cout <<
"Median BMI = ";
198 int i(tmpbmis.size() / 2);
199 if (tmpbmis.size() % 2 == 0)
200 std::cout << (tmpbmis.at(i) + tmpbmis.at(i-1)) / 2.0 <<
'\n';
202 std::cout << tmpbmis.at(i) <<
'\n';
209 std::locale::global(std::locale{
""});
210 std::cout.imbue(std::locale{});
211 std::cin.imbue(std::locale{});
213 std::vector<std::string> names{};
214 std::vector<int> heights{};
215 std::vector<int> weights{};
216 std::vector<char> sexes{};
217 std::vector<int> bmis{};
220 std::cout <<
"Enter threshold BMI: ";
221 if (not (std::cin >> threshold))
225 std::cout <<
"Enter name, height (in cm),"
226 " and weight (in kg) for each person:\n";
227 while (
get_record(names, heights, weights, sexes))
229 int bmi{
compute_bmi(heights.back(), weights.back())};
231 std::cout <<
"BMI = " << bmi <<
'\n';
235 std::cout <<
"\n\nMale data\n";
236 print_table(
'M', heights, weights, bmis, sexes, names, threshold);
237 std::cout <<
"\nFemale data\n";
238 print_table(
'F', heights, weights, bmis, sexes, names, threshold);
int main()
Main program to compute BMI.
void skip_line()
Skip the rest of the input line.
void print_table(char sex, std::vector< int > const &heights, std::vector< int > const &weights, std::vector< int > const &bmis, std::vector< char > const &sexes, std::vector< std::string > const &names, int threshold)
Print a table. Print a table of height, weight, sex, BMI, and name. Print only records for which sex ...
int compute_bmi(int height, int weight)
Compute the body-mass index (BMI). The height is in centimeters, so the computed value is off by 10...
bool get_record(std::vector< std::string > &names, std::vector< int > &heights, std::vector< int > &weights, std::vector< char > &sexes)
Get a single person's record. Store the name, height, weight, and sex in the supplied vectors...