Source of switch.cpp


  1: //switch.cpp
  2: //Illustrates the switch-statement.

  4: #include <iostream>
  5: using namespace std;

  7: int main()
  8: {
  9:     cout << "\nThis program gets a user's opinion of Microsoft "
 10:         "(1 = very low, 5 = very high).\n\n";

 12:     int opinion;

 14:     cout << "Enter a value from 1 to 5: ";
 15:     cin >> opinion;

 17:     switch (opinion)
 18:     {
 19:         case 1:
 20:             cout << "That's OK. I won't tell Bill!\n";
 21:             break;

 23:         case 2:
 24:             cout << "Not so high, eh? You and many other users!\n";
 25:             break;

 27:         case 3:
 28:             cout << "Hmmm! A fence sitter on the subject, eh?\n";
 29:             break;

 31:         case 4:
 32:             cout << "Oh yeah? What have they done for you lately?\n";
 33:             break;

 35:         case 5:
 36:             cout << "The cheque from Bill is in the mail!\n";
 37:             break;

 39:         default:
 40:         {
 41:             if (opinion > 5)
 42:                 cout << "Your opinion is off-scale!!\n";
 43:             else
 44:                 cout << "Now, now, now ... !\n";
 45:         }
 46:     }
 47:     cout << endl;
 48: }