From 3299b740d2e35e2eb9ec68bbc5d1f93dc28b8c06 Mon Sep 17 00:00:00 2001
From: Christian Wunder <cw631063@fh-muenster.de>
Date: Sat, 18 Jan 2020 16:03:43 +0100
Subject: [PATCH] Fix Modulo zero.

---
 src/calc.cpp  | 7 ++++++-
 test/test.cpp | 6 ++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/calc.cpp b/src/calc.cpp
index 7b6a0d6..b396bb5 100644
--- a/src/calc.cpp
+++ b/src/calc.cpp
@@ -8,7 +8,12 @@ int plus(    int a, int b) { return a + b; }
 int minus(   int a, int b) { return a - b; }
 int multiply(int a, int b) { return a * b; }
 int power(   int a, int b) { return std::pow(a, b); }
-int modulo(  int a, int b) { return a % b; }
+int modulo(  int a, int b) { 
+  if(b == 0) {
+    throw std::domain_error("Modulo by 0 not defined.");
+  }
+  return a % b;
+}
 int divide(  int a, int b) { 
   if(b == 0) {
     throw std::domain_error("Division by 0.");
diff --git a/test/test.cpp b/test/test.cpp
index 67162bb..4551d54 100644
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -67,3 +67,9 @@ TEST_CASE( "Unrecognized characters are detected and exception is thrown", "[cal
   REQUIRE_THROWS( evaluate("11+3?4") );
   REQUIRE_THROWS( evaluate("1F+") );
 }
+
+TEST_CASE( "modulo by zero is detected and exception is thrown", "[calc]") {
+   REQUIRE_THROWS( evaluate("40%") );
+   REQUIRE_THROWS( evaluate("00%") );
+   REQUIRE_THROWS( evaluate("90%") );
+}
-- 
GitLab