From dd658c9c1dd2d396b5c9da05289045cf94f2878a Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sat, 9 Mar 2024 21:01:44 -0500 Subject: [PATCH] WROTE IN AIRPLANE: Checked edge case where the number is zero --- numeric_base.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/numeric_base.cpp b/numeric_base.cpp index 309265f..389fa72 100644 --- a/numeric_base.cpp +++ b/numeric_base.cpp @@ -26,12 +26,17 @@ unsigned int to_decimal(std::string num, int from_base) { std::string from_decimal(unsigned int num, int to_base) { std::string return_val; int val = 0; - while (num > 0) { - val = num % to_base; - return_val.push_back(possible_chars[val]); - num /= to_base; + /* Handle the special case of num being zero: In this case, the result is also zero */ + if (num == 0) { + return_val = "0"; + } else { + while (num > 0) { + val = num % to_base; + return_val.push_back(possible_chars[val]); + num /= to_base; + } } - + /* Reverse the string, since we started from the right */ std::reverse(return_val.begin(), return_val.end());