Source of bool_data.cpp


  1: //bool_data.cpp
  2: //Illustrates the "bool" data type, boolean variables,
  3: //and evaluation of boolean expressions.

  5: #include <iostream>
  6: #include <iomanip>
  7: using namespace std;

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the boolean data type, use of "
 12:         "boolean variables,\nand the evaluation of boolean expressions. "
 13:         "Be sure to study the source code.\n\n";


 16:     cout << "Some relational expressions involving "
 17:         "just constants, and their values:\n"
 18:         "Expression: 7 == 3   'A' != 'a'   4.6 > 9.3\n"
 19:         "Value: " << setw(8)  << (7 == 3)
 20:                   << setw(11) << ('A' != 'a')
 21:                   << setw(13) << (4.6 > 9.3) << endl << endl;


 24:     cout << "Some compound boolean expressions involving "
 25:         "just constants, and their values:\n"
 26:         "Expression: 7 == 3  &&  -5.3 <= -3.5      "
 27:         "2 == 6  ||  'A' != 'a'    \n"
 28:         "Value: " << setw(14) << (7 == 3  &&  -5.3 <= 3.5)
 29:                   << setw(30) << (2 == 6  ||  'A' != 'a') << endl
 30:         << boolalpha <<
 31:         "Value: " << setw(14) << (7 == 3  &&  -5.3 <= 3.5)
 32:                   << setw(30) << (2 == 6  ||  'A' != 'a') << endl
 33:         << noboolalpha <<
 34:         "Value: " << setw(14) << (7 == 3  &&  -5.3 <= 3.5)
 35:                   << setw(30) << (2 == 6  ||  'A' != 'a')
 36:         << endl << endl;


 39:     int menuChoice;
 40:     bool choiceIsValid; //Declaration of a "boolean variable"

 42:     cout << "Enter a \"menu choice\" value from 1 to 5: ";
 43:     cin >> menuChoice;  cin.ignore(80, '\n');  cout << endl;

 45:     choiceIsValid = (menuChoice >= 1  &&  menuChoice <= 5);
 46:     cout << "Your menu choice was valid (1) or invalid (0), "
 47:         "depending on this value: " << choiceIsValid;
 48:     cout << endl << endl;
 49: }