This Blog is dedicated to Sir Johnson Lin
Learn flow chart from C language , master C with the chart!

C++ 基本 使用多重選擇功能 C++ basic function , loop decision (switch) introduction



D:\cppCodes\HelloSwitch\HelloSwitch.c++
 1 //++++++++++++++++++++++++++
 2  //      HelloSwitch.cpp
 3  //++++++++++++++++++++++++++
 4  
 5  ///#include headers
 6  #include <iostream>
 7  
 8  ///USING
 9  using namespace std;
10  
11  ///mainentry
12  int main()
13  {
14      ///declare variables
15      int iMyInt = 88;               // Integer
16      float fMyFloat = 1.68;    // Floating point number
17      char cMyChar = 'A';         // Character
18      string sMyString = "my text string ";     // txt string
19      char caMyString[] = "my character array"; // txt string
20      char cMyInputChar   ;         // Character
21      
22      ///Prompt Input
23      cout << "Key '1' to display once " << endl ;
24      cout << "Key '2' to display twice " << endl ;
25      cout << "Key \"other\" to display none" << endl ;
26      cout <<  "=  " ;
27      
28      ///Keyin
29      cin >> cMyInputChar;
30      
31      ///switch cMyInputChar
32      switch(cMyInputChar)
33      {
34          ///Keyin 1
35          case '1' :
36          {
37              cout << endl;
38              cout << "Int = " << iMyInt << endl;
39              cout << "Float = " << fMyFloat << endl;
40              cout << "Char = " << cMyChar << endl;
41              cout << "String = " << sMyString << endl;
42              cout << "CharArray = " << caMyString << endl;
43              break;
44          }
45          
46          ///Keyin 2
47          case '2' :
48          {
49              cout << endl;
50              cout << "Int = " << iMyInt << endl;
51              cout << "Float = " << fMyFloat << endl;
52              cout << "Char = " << cMyChar << endl;
53              cout << "String = " << sMyString << endl;
54              cout << "CharArray = " << caMyString << endl;
55              cout << endl;
56              cout << "Int = " << iMyInt << endl;
57              cout << "Float = " << fMyFloat << endl;
58              cout << "Char = " << cMyChar << endl;
59              cout << "String = " << sMyString << endl;
60              cout << "CharArray = " << caMyString << endl;
61              break;
62          }
63          
64          ///default
65          default:
66          cout << "Oh Oh !" << endl;
67          
68          ///+
69      }//end Switch
70      
71      ///mainend
72      return 0;
73  }//end main()
74  

@ 下載程式碼 Download source code

沒有留言:

張貼留言