Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////
- // OVERLOADED STREAM EXTRACTION OPERATOR '>>' (input) /////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- istream& operator>>(istream& iStreamObj, fractionType& fracObj)
- {
- // NO EXCEPTION HANDLING //////////////////////////////////////////////////////
- /*
- cout << " CALLING OVERLOADED STREAM EXTRACTION OPERATOR: >> (input) \n";
- cout << " ENTER NUMERATOR: ";
- iStreamObj >> fracObj.numer;
- cout << " ----------\n";
- cout << " ENTER DENOMINATOR: ";
- iStreamObj >> fracObj.denom;
- return iStreamObj;
- */
- // ATTEMPT #1 -- BROKEN EXCEPTION HANDLING //////////////////////////////////
- cout << " CALLING OVERLOADED STREAM EXTRACTION OPERATOR: >> (input) \n";
- try
- {
- cout << " ENTER NUMERATOR: ";
- iStreamObj >> fracObj.numer;
- cout << " ----------\n";
- cout << " ENTER DENOMINATOR: ";
- iStreamObj >> fracObj.denom;
- if (fracObj.denom == 0)
- {
- cout << " fracObj.denom == 0\t";
- throw fracObj.denom;
- }
- if (fracObj.denom < 0)
- {
- cout << " fracObj.denom < 0\t";
- throw string("Negative denominator!");
- }
- if ((fracObj.numer <= 255) && (isalpha(fracObj.numer) != 0))
- {
- cout << " fracObj.numer <= 255 && isalpha(fracObj.numer) != 0\t";
- throw static_cast<char>(fracObj.numer);
- }
- if ((fracObj.denom <= 255) && (isalpha(fracObj.denom) != 0))
- {
- cout << " fracObj.denom <= 255 && isalpha(fracObj.denom) != 0\t";
- throw static_cast<char>(fracObj.denom);
- }
- return iStreamObj;
- }
- catch (int err) // WORKING
- {
- cout << " ERROR! -- ZERO DENOMINATOR NOT ALLOWED! \n";
- fracObj.numer = NULL;
- fracObj.denom = NULL;
- }
- catch (string err) // NOT WORKING ALL THE TIME
- {
- cout << " ERROR! -- NEGATIVE DENOMINATOR NOT ALLOWED! \n";
- fracObj.numer = NULL;
- fracObj.denom = NULL;
- }
- catch (...) // NOT WORKING
- {
- cout << " ERROR! -- INVALID DATA TYPE! ONLY INTEGERS ALLOWED! \n";
- fracObj.numer = NULL;
- fracObj.denom = NULL;
- //cin.clear(); // DOES NOT WORK
- //cin.ignore(numeric_limits<streamsize>::max(), '\n'); // DOES NOT WORK
- //iStreamObj.clear(); // DOES NOT WORK
- //iStreamObj.ignore(numeric_limits<streamsize>::max(), '\n'); // DOES NOT WORK
- }
- // ATTEMPT #2 -- ALTERNATE METHOD? //////////////////////////////////////////
- /*
- cout << " CALLING OVERLOADED STREAM EXTRACTION OPERATOR: >> (input) \n";
- cout << " ENTER NUMERATOR: ";
- iStreamObj >> fracObj.numer;
- cout << " ----------\n";
- cout << " ENTER DENOMINATOR: ";
- iStreamObj >> fracObj.denom;
- return fractionType(fracObj.numer, fracObj.denom); // TRYING TO USE CONSTRUCTOR
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement