My teacher has assigned a program to use both if-else statements and switch statements, so we understand how to implement both. The program asked us to prompt the user to input their weight and height in pounds and meters respectively. This is my attempt:

Without the switch

#include "stdafx.h" #include <iostream> using namespace std; int main() { double height, weight, BMI, heightMeters, weightKilo; const double KILOGRAMS_PER_POUND = 0.45359237; const double METERS_PER_INCH = 0.0245; cout << "Please enter your height (inches) and weight (pounds)" << endl; cin >> height >> weight; weightKilo = weight*KILOGRAMS_PER_POUND; heightMeters = height*METERS_PER_INCH; BMI = weightKilo / (heightMeters*heightMeters); if (BMI < 18.5) { cout << "You are underweight " << endl; } else if (BMI >= 18.5 && BMI < 25.0) { cout << "You are normal" << endl; } else if (BMI >= 25.0 && BMI < 30.0) { cout << "You are overweight" << endl; } else if (BMI >= 30.0 && BMI < 35) { cout << "You are obese" << endl; } else { cout << "You are gravely overweight" << endl; } }

With the switch

#include "stdafx.h" #include <iostream> using namespace std; int main() { double height, weight, heightMeters, weightKilo; int BMI, q; const double KILOGRAMS_PER_POUND = 0.45359237; const double METERS_PER_INCH = 0.0245; cout << "Please enter your height (inches) and weight (pounds)" << endl; cin >> height >> weight; weightKilo = weight*KILOGRAMS_PER_POUND; heightMeters = height*METERS_PER_INCH; BMI = weightKilo / (heightMeters*heightMeters); if (BMI < 18.5) { q = 1; } else if (BMI >= 18.5 && BMI < 25.0) { q = 2; } else if (BMI >= 25.0 && BMI < 30.0) { q = 3; } else if (BMI >= 30.0 && BMI < 35) { q = 4; } else { q = 5; } switch (q) { case 1: cout << "You are underweight" << endl; break; case 2: cout << "You are a normal weight " << endl; break; case 3: cout << "You are overweight" << endl; break; case 4: cout << "You are obese" << endl; break; case 5: cout << "You are gravely overweight" << endl; break; } }

This was the way I thought of, to include a switch statement. Is there some way to implement the first code block to just a switch statement?

I was almost certain that it could not be done to use ranges nor to use doubles (18.5). I emailed my teacher and they gave me an answer along the lines of

It may not make sense to you, but sometimes you are going to have to write a program that does not make sense. I am not saying that you don't have legitimate questions, but if anyone can figure it out you can. But then, maybe it can't be figured out. That's the challenge".

So, I'm asking: Is there some method to just use a switch statement for the first code block, or is what I did the best way to use a switch statement in the code, even if it is in no way necessary?