Added division by 0 error checking
This commit is contained in:
+13
-1
@@ -1,4 +1,5 @@
|
|||||||
#include "basecalc.h"
|
#include "basecalc.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
BaseCalculator::~BaseCalculator()
|
BaseCalculator::~BaseCalculator()
|
||||||
{}
|
{}
|
||||||
@@ -20,10 +21,21 @@ int BaseCalculator::calculate(const Operation& op)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case OpType::Division:
|
case OpType::Division:
|
||||||
result = op.a / op.b;
|
if (op.b == 0)
|
||||||
|
{
|
||||||
|
std::cerr << "Invalid operation requested! Resetting the accumulator to 0";
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
result = op.a / op.b;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OpType::Modulo:
|
case OpType::Modulo:
|
||||||
|
if (op.b == 0)
|
||||||
|
{
|
||||||
|
std::cerr << "Invalid operation requested! Resetting the accumulator to 0";
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
result = op.a % op.b;
|
result = op.a % op.b;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user