C++ examples for Statement:switch
Use switch statement to output n-th day
#include <iostream> #include <string> std::string getDay(int); void printVerse(int); int main(int argc, const char *argv[]) { for (int i = 1; i <= 12; i++) { std::cout << "\nOn the " << getDay(i)<< " day \n"; printVerse(i);/*from w ww. ja va 2s .c o m*/ } return 0; } std::string getDay(int day) { switch (day) { case 1: return "First"; case 5: return "Fifth"; case 6: return "Sixth"; case 7: return "Seventh"; case 8: return "Eighth"; case 9: return "Nineth"; case 10: return "Tenth"; case 11: return "Eleventh"; case 12: return "Twelveth"; default: return ""; } } void printVerse(int day) { switch (day) { case 12: std::cout << "Twelve " << std::endl; case 11: std::cout << "Eleven" << std::endl; case 10: std::cout << "Ten" << std::endl; case 9: std::cout << "Nine" << std::endl; case 8: std::cout << "Eight" << std::endl; case 7: std::cout << "Seven" << std::endl; case 6: std::cout << "Six" << std::endl; case 5: std::cout << "Five" << std::endl; case 4: std::cout << "Four" << std::endl; case 3: std::cout << "Three" << std::endl; case 2: std::cout << "Two" << std::endl; case 1: printf("%s Partridge in a Pear Tree\n",(day > 1) ? "and a" : "a"); default: break; } }