Source of bool_errors.cpp


  1: //bool_errors.cpp
  2: //The purpose of this program is to highlight some of pitfalls to 
  3: //avoid when dealing with boolean data values and testing conditions.

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

  8: int main()
  9: {
 10:     cout << "\nThis program is designed to freak you out, and "
 11:         "serve as a warning.\n\nHere's what you should do:\n"
 12:         "1. On the first run, enter the values 6, then 3 and 4, "
 13:         "then 1.2, 2.3 and 3.4."
 14:         "\n2. Now check the source code and reconcile "
 15:         "the input and output."
 16:         "\n3. On the second run, enter the values 7, then 8 and 9, "
 17:         "then 3.4, 2.3 and 1.2 "
 18:         "\n4. Now check the source code and reconcile "
 19:         "the input and output once more.\n";

 21:     int i, j;

 23:     cout << "\nEnter an integer: ";
 24:     cin >> i;  cin.ignore(80, '\n');
 25:     if (i = 6)  //First error: What is it?
 26:         cout << "The value entered was 6.\n";


 29:     cout << "\nEnter two more integers: ";
 30:     cin >> i >> j;  cin.ignore(80, '\n');
 31:     if (i == 3 || 4)  //Second error: What is it?
 32:         cout << "The first value entered was either 3 or 4.\n";
 33:     else
 34:         cout << "The first value entered was neither 3 nor 4.\n";

 36:     if (i || j == 4)  //Third error: What is it?
 37:         cout << "One of the values entered was 4.\n";
 38:     else
 39:         cout << "Neither value entered was 4.\n";


 42:     double x, y, z;

 44:     cout << "\nEnter three real numbers: ";
 45:     cin >> x >> y >> z;  cin.ignore(80, '\n');
 46:     if (x < y < z)  //Fourth error: What is it?
 47:         cout << "The values were entered in increasing order.\n";
 48:     else
 49:         cout << "The values were not entered in increasing order.\n";
 50:     cout << endl;
 51: }