0

I'm stuck, I'm not really sure how to use a switch statement to switch "DESKTOP" to "desktop" for example.

enum ComputerType { DESKTOP, LAPTOP, TABLET, HANDHELD }; 

// Prints a computer type as a lower case string.
// Use switch statement to implement this function.
// params: (in)
   void PrintComputerType( ComputerType comp ) const
   {
      switch ( comp )
      {




      }
   }

3 Answers3

0

Switch statements are a form of flow control, and the switch argument is unrelated to the actual operation performed in the case statement. Below is an example of how it can be used to output "desktop" if comp is the DESKTOP enum value. But there's nothing stopping you from calling cout << "fish" instead or doing something unrelated to strings altogether.

switch(comp)
{
case DESKTOP:
  cout << "desktop" << endl;
  break;
case LAPTOP:
  cout << "laptop" << endl;
  break;
}
MooseBoys
  • 6,641
  • 1
  • 19
  • 43
0

Nothing special.

void PrintComputerType( ComputerType comp ) const
{
   switch ( comp )
   {
     case DESKTOP:  cout << "desktop";     break;
     case LAPTOP:   cout << "laptop";      break;
     case TABLET:   cout << "tablet";      break;
     case HANDHELD: cout << "handled";     break;
     default:       cout << "wrong input"; break;
   }
}
Deck
  • 1,969
  • 4
  • 20
  • 41
0

The enum values are basically ints, so they can be used as cases in the switch statement.

switch ( comp )
{
  case DESKTOP:
    cout << "Desktop\n";
    break;
  case LAPTOP:
    cout << "Laptop\n";
    break;
  case TABLET:
    cout << "Tablet\n";
    break;
  case HANDHELD:
    cout << "Handheld\n";
    break;
  default:
    cout << "Invalid Selection\n";
    break;
}
Zero Fiber
  • 4,417
  • 2
  • 23
  • 34