Source of dangling_else.cpp


  1: //dangling_else.cpp
  2: //Illustrates the "dangling else" problem.

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

  7: int main()
  8: {
  9:     cout << "\nThis program allows you to analyze the dangling else "
 10:         "problem.\nStudy the source code to predict the output for "
 11:         "each of these input values:\n9, 5, and 0\n\n";

 13:     int numberOfDays;

 15:     cout << "Enter the number of days it has rained without stopping: ";
 16:     cin >> numberOfDays;  cin.ignore(80, '\n');  cout << endl;

 18:     cout << "\nStart of first construct:  ";
 19:     if (numberOfDays > 1)
 20:         if (numberOfDays <= 7)
 21:             cout << "Get out your umbrellas!\n";
 22:         else
 23:             cout << "Run for higher ground!\n";

 25:     cout << "\nStart of second construct: ";
 26:     if (numberOfDays <= 7)
 27:         if (numberOfDays > 1)
 28:             cout << "Get out your umbrellas!\n";
 29:     else
 30:         cout << "Run for higher ground!\n";
 31:     cout << endl;
 32: }