switch statement based on data type : switch « Operators statements « C++ Tutorial






#include <iostream.h>

void increase (void* data, int type)
{
  switch (type)
  {
    case sizeof(char) : (*((char*)data))++; break;
    case sizeof(short): (*((short*)data))++; break;
    case sizeof(long) : (*((long*)data))++; break;
  }
}

int main ()
{
  char a = 5;
  short b = 9;
  long c = 12;
  increase (&a,sizeof(a));
  increase (&b,sizeof(b));
  increase (&c,sizeof(c));
  cout << (int) a << ", " << b << ", " << c;
  return 0;
}
6, 10, 13"








3.14.switch
3.14.1.The switch
3.14.2.A switch without break statements.
3.14.3.switch case falling through
3.14.4.char navigation based on switch
3.14.5.A Help system that process multiple requests.
3.14.6.switch statement based on data type
3.14.7.Calculator based on switch statement