Source of odd_squares.cpp


  1: //odd_squares.cpp
  2: //Outputs squares of odd integers input by the user. Also counts and
  3: //reports number of odd values seen. Illustrates both a count-controlled
  4: //loop and a second counter that counts something else (odd integers).

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

  9: const bool DEBUGGING_ON = false;

 11: int main()
 12: {
 13:     cout << "\nThis program outputs the square of any odd\ninteger "
 14:         "entered, but ignores all even values.\nIt also indicates how "
 15:         "many odd values were seen.\nAll input values must be *positive* "
 16:         "integers.\n\n";

 18:     int numberOfValues;
 19:     cout << "Enter the number of data values to be entered: ";
 20:     cin >> numberOfValues;  cin.ignore(80, '\n');  cout << endl;

 22:     int i;
 23:     bool iIsOdd;
 24:     int oddCount = 0; //Initialize count of odd input values properly

 26:     for (int loopCount = 1; loopCount <= numberOfValues; loopCount++)
 27:     {
 28:         cout << "\nEnter a data value here: ";
 29:         cin >> i;  cin.ignore(80, '\n');
 30:         iIsOdd = (i%2 == 1); //Check to see if value read is odd
 31:         if (iIsOdd)
 32:         {
 33:             cout << i << " squared is " << i*i << ".\n";
 34:             ++oddCount;
 35:         }
 36:         else
 37:             cout << "That one wasn't odd." << endl;
 38:         if (DEBUGGING_ON)
 39:         {
 40:             cout << "loopCount: " << loopCount << "   "
 41:                  << "i: "         << i         << "   "
 42:                  << "iIsOdd: "    << iIsOdd    << "   "
 43:                  << "oddCount: "  << oddCount  << endl << endl;
 44:         }
 45:     }
 46:     cout << "\nThe total number of odd integers was "
 47:          << oddCount << ".\n";
 48:     cout << endl;
 49: }