CPP/Switch

From ProgrammingExamples
< CPP
Revision as of 13:38, 26 June 2010 by Daviddoria (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Switch.cpp

#include <iostream>
#include <string>
 
int main(int argc, char *argv[])
{
  //switch with ints
  int test = 1;
  switch ( test ) 
  {
  std::cout << "before cases" << std::endl;
  case 1 : 
    std::cout << 1 << std::endl;
    break;
  case 2 : 
    std::cout << 2 << std::endl;
    break;
  default : 
    std::cout << "default" << std::endl;
  }
 
  //switch with chars
  char a = 'a';
  switch (a) 
  {
  std::cout << "before cases" << std::endl;
  case 'a' : 
    std::cout << "yes" << std::endl;
    break;
  case 'b' : 
    std::cout << "no" << std::endl;
    break;
  default : 
    std::cout << "no" << std::endl;
  }
 
  /*
  //switch with strings (this is NOT supposed to compile)
  //the error is "switch quantity inot an integer"
  std::string testString = "test";
  switch (testString) 
  {
  std::cout << "before cases" << std::endl;
  case "test" : 
    std::cout << "yes" << std::endl;
    break;
  case "something else" : 
    std::cout << "no" << std::endl;
    break;
  default : 
    std::cout << "no" << std::endl;
  }
  */
 
  return 0;
}