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";

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.