For a programming class, I have to convert a range of values to a switch stateme
ID: 670497 • Letter: F
Question
For a programming class, I have to convert a range of values to a switch statement without using if/else ifs. Here are the values that I need to convert to cases:
0 to 149 ............. $10.00
150 to 299 .........$15.00
300 to 449 .........$25.00
550 to 749..........$40.00
750 to 1199........$65.00
2000 and above.....$85.00
I am having difficulty finding a way to separate the values since they are so close in number (like 149 to 150).
I have used plenty of algorithms such as dividing the input by 2000, and then multiplying that by 10 to get a whole number, but they are too close to each other to create a new case for.
Using C++ programming
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main() {
int value = 130;
switch (value) {
case 0 ... 149:
cout << "$10";
break;
case 150 ... 299:
cout << "$15";
break;
case 300 ... 449:
cout << "$25";
break;
case 550 ... 749:
cout << "$40";
break;
case 750 ... 1199:
cout << "$65";
break;
case 2000 ... 10000 :
cout << "$85";
break;
default:
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.