Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // OVERLOADED STREAM EXTRACTION OPERATOR '>>' (input) /////////////////////////
  3. ///////////////////////////////////////////////////////////////////////////////
  4. istream& operator>>(istream& iStreamObj, fractionType& fracObj)
  5. {
  6.  
  7.  
  8. // NO EXCEPTION HANDLING //////////////////////////////////////////////////////
  9. /*
  10.     cout << " CALLING OVERLOADED STREAM EXTRACTION OPERATOR: >> (input) \n";
  11.     cout << " ENTER NUMERATOR:        ";
  12.     iStreamObj >> fracObj.numer;
  13.     cout << "                     ----------\n";
  14.     cout << " ENTER DENOMINATOR:      ";
  15.     iStreamObj >> fracObj.denom;
  16.     return iStreamObj;
  17. */
  18.  
  19.  
  20. // ATTEMPT #1  --  BROKEN EXCEPTION HANDLING //////////////////////////////////
  21.  
  22.     cout << " CALLING OVERLOADED STREAM EXTRACTION OPERATOR: >> (input) \n";
  23.     try
  24.     {
  25.         cout << " ENTER NUMERATOR:        ";
  26.         iStreamObj >> fracObj.numer;
  27.         cout << "                     ----------\n";
  28.         cout << " ENTER DENOMINATOR:      ";
  29.         iStreamObj >> fracObj.denom;
  30.  
  31.         if (fracObj.denom == 0)
  32.         {
  33.             cout << " fracObj.denom == 0\t";
  34.             throw fracObj.denom;
  35.         }
  36.         if (fracObj.denom < 0)
  37.         {
  38.             cout << " fracObj.denom < 0\t";
  39.             throw string("Negative denominator!");
  40.         }
  41.         if ((fracObj.numer <= 255) && (isalpha(fracObj.numer) != 0))
  42.         {
  43.             cout << " fracObj.numer <= 255 && isalpha(fracObj.numer) != 0\t";
  44.             throw static_cast<char>(fracObj.numer);
  45.         }
  46.         if ((fracObj.denom <= 255) && (isalpha(fracObj.denom) != 0))
  47.         {
  48.             cout << " fracObj.denom <= 255 && isalpha(fracObj.denom) != 0\t";
  49.             throw static_cast<char>(fracObj.denom);
  50.         }
  51.        
  52.        
  53.  
  54.         return iStreamObj;
  55.     }
  56.     catch (int err)                 // WORKING
  57.     {
  58.         cout << " ERROR!  --  ZERO DENOMINATOR NOT ALLOWED! \n";
  59.         fracObj.numer = NULL;
  60.         fracObj.denom = NULL;
  61.     }
  62.     catch (string err)              // NOT WORKING ALL THE TIME
  63.     {
  64.         cout << " ERROR!  --  NEGATIVE DENOMINATOR NOT ALLOWED! \n";
  65.         fracObj.numer = NULL;
  66.         fracObj.denom = NULL;
  67.     }
  68.     catch (...)                     // NOT WORKING
  69.     {
  70.         cout << " ERROR!  --  INVALID DATA TYPE! ONLY INTEGERS ALLOWED! \n";
  71.         fracObj.numer = NULL;
  72.         fracObj.denom = NULL;
  73.  
  74.         //cin.clear();                                                  // DOES NOT WORK
  75.         //cin.ignore(numeric_limits<streamsize>::max(), '\n');          // DOES NOT WORK
  76.         //iStreamObj.clear();                                           // DOES NOT WORK
  77.         //iStreamObj.ignore(numeric_limits<streamsize>::max(), '\n');   // DOES NOT WORK
  78.     }
  79.  
  80.  
  81.  
  82. // ATTEMPT #2  --  ALTERNATE METHOD? //////////////////////////////////////////
  83. /*
  84.     cout << " CALLING OVERLOADED STREAM EXTRACTION OPERATOR: >> (input) \n";
  85.     cout << " ENTER NUMERATOR:        ";
  86.     iStreamObj >> fracObj.numer;
  87.     cout << "                     ----------\n";
  88.     cout << " ENTER DENOMINATOR:      ";
  89.     iStreamObj >> fracObj.denom;
  90.     return fractionType(fracObj.numer, fracObj.denom);      // TRYING TO USE CONSTRUCTOR
  91. */
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement