Home > OS >  Throw a ZeroCheck exception object if the denominator is 0
Throw a ZeroCheck exception object if the denominator is 0

Time:03-22

I wrote this program which performs mathematical operations. And Well…, I tried to add a new header file “ZeroCheck.h” ..., and create (declare and define) an exception class ZeroCheck which displays the message "Denominator is 0, invalid division." via the what() member function. And I wanted to modify the main() function by creating Math3(6,0) object and using the try statement to output Mathematics objects. The ZeroCheck exception should be caught in catch block and the error message should be displayed if the division is invalid. But unfortunately, the output is not exactly the same as what I expect, I mean when I run my program, the output is this:

main.cpp

#include <iostream>
#include <cstdlib>

#include "Mathematics.h"
#include "ZeroCheck.h"

// using std::cout, std::cerr, std::ostream;
using namespace std;


int
main()
{
    try {
        Mathematics<int> Math1(10, 5);
        Mathematics<double> Math2(5.5, 3.4);
        Mathematics<int> Math3(6, 0);

        cout << "Math 1:" << '\n';
        cout << Math1 << '\n' << '\n';

        cout << "Math 2:" << '\n';
        cout << Math2 << '\n';

        cout << "Math 3" << '\n';
        cout << Math3 << '\n';

    } catch (ZeroCheck e) {
        cerr << e.what() << '\n';
    }

    return (EXIT_SUCCESS);
}


template <class T>
Mathematics<T>::Mathematics(T v1, T v2) : val1(v1), val2(v2) { }


template <class T>
T Mathematics<T>::addition()
{
    return (val1   val2);
}


template <class T>
T Mathematics<T>::subtraction()
{
    return (val1 - val2);
}


template <class T>
T Mathematics<T>::multiplication()
{
    return (val1 * val2);
}


template <class T>
T Mathematics<T>::division()
{
    if (val2 == 0) {
        throw ZeroCheck("Denominator is 0, invalid division.");
    }

    return (val1 / val2);
}


template<class T>
ostream& operator<<(ostream& os, Mathematics<T>& obj)
{
    os << "The result of calculation for: " << obj.val1 << " and " << obj.val2 << '\n'
       << "Sum is: " << obj.addition() << '\n'
       << "Difference is: " << obj.subtraction() << '\n'
       << "Product is: " << obj.multiplication() << '\n'
       << "Quotient is: " << obj.division() << '\n';

    return (os);
}

Mathematics.h

#ifndef MATHEMATICS_H_INCLUDED
#define MATHEMATICS_H_INCLUDED

#include <iostream>

using std::ostream;


template <class T>
class Mathematics {

    public:
        Mathematics(T, T);

        T addition();
        T subtraction();
        T multiplication();
        T division();

        template <class U>friend ostream& operator<<(ostream&, Mathematics<U>&);

    private:
        T val1;
        T val2;

};

#endif  /* MATHEMATICS_H_INCLUDED */

ZeroCheck.h

#ifndef ZEROCHECK_H_INCLUDED
#define ZEROCHECK_H_INCLUDED

#include <string>

using std::string;


class ZeroCheck {

    public:
        ZeroCheck()
        {
            this->msg = "";
        }


        ZeroCheck(string _msg)
        {
            this->msg = _msg;
        }

        string what()
        {
            return (msg);
        }

    private:
        string msg;
};

#endif  /* ZEROCHECK_H_INCLUDED */

But unfortunately, the output is not exactly the same as what I expect, I mean when I run my program, the output is this:

Math 1:                                                                                                                                         
The result of calculation for: 10 and 5   
Sum is: 15      
Difference is: 5  
Product is: 50                                  
Quotient is: 2                                                                                                                                  

      
Math 2:                               
The result of calculation for: 5.5 and 3.4
Sum is: 8.9     
Difference is: 2.1
Product is: 18.7                                
Quotient is: 1.61765                                                                                                                            

Math 3
The result of calculation for: 6 and 0
Sum is: 6
Difference is: 6
Product is: 0
// As you can see here the output is: 'Quotient is: Denominator is 0, invalid division.' Instead of just 'Denominator is 0, invalid division.'
Quotient is: Denominator is 0, invalid division.

This is the output that I expect:

Math 1:                                                                                                                                                                              
The result of calculation for: 10 and 5
Sum is: 15                                       
Difference is: 5
Product is: 50                                             
Quotient is: 2                            
                
                  
Math 2:            
The result of calculation for: 5.5 and 3.4
Sum is: 8.9       
Difference is: 2.1                               
Product is: 18.7
Quotient is: 1.61765


Math 3:            
The result of calculation for: 6 and 0
Sum is: 6
Difference is: 6                               
Product is: 0
Denominator is 0, invalid division.

Any idea what I can do to get the output I want? (Note: I have only tried to do what I could with the instructions provided, and by the way if you see something wrong with my code, I apologize in advance, I'm not familiar with some things yet, so...).

CodePudding user response:

template<class T>
ostream& operator<<(ostream& os, Mathematics<T>& obj)
{
    os << "The result of calculation for: " << obj.val1 << " and " << obj.val2 << '\n'
    << "Sum is: " << obj.addition() << '\n'
    << "Difference is: " << obj.subtraction() << '\n'
    << "Product is: " << obj.multiplication() << '\n';
    if (obj.val1 == 0 || obj.val2 == 0)
    {
        os << obj.division() << '\n';
    }
    else
    {
        os << "Quotient is: " << obj.division() << '\n';
    }
    return (os);
}

You probably already figured out the answer to your question but I'd figured I'd share my response anyways. There might be a better way to check the condition but I think it looks good and it's easy to read! Good luck!!

  • Related