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