0

I need help with a conversion this to a switch statement, I haven't done one before that is why im asking

Convert the following C++ if statement to equavalent switch statement:

if (SalesType >= 4 &&  SalesType <= 5)
{
    TypeName = "Weekly Special";
    DiscountRate = 0.45;
}
else if (SalesType < 4 && SalesType >= 2)
         {
             TypeName = "Seasonal Sale";
             DiscountRate = 0.55;
          }
       else if (SalesType == 1)
              {
                  TypeName = "Annual Sale";
                  DiscountRate = 0.60;
              }
             else
                  cout << "Invalid Sales Type. \n";
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Narue
0

Just so you know, a switch isn't well suited to selecting ranges. As the range grows, your switch will become progressively more complicated because you have to specify every possible value in the range. Here's the equivalent switch:

switch ( SalesType ) {
case 5:
case 4:
  TypeName = "Weekly Special";
  DiscountRate = 0.45;
  break;
case 3:
case 2:
  TypeName = "Seasonal Sale";
  DiscountRate = 0.55;
  break;
case 1:
  TypeName = "Annual Sale";
  DiscountRate = 0.60;
  break;
default:
  cout << "Invalid Sales Type. \n";
  break;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.