From 000d6da85fac2e5cf070052589b5b71846661758 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 28 Aug 2020 15:13:13 -0400 Subject: [PATCH] Updated 17 to throw exception --- Headers/Problems/Problem17.hpp | 1 + src/Problems/Problem17.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Headers/Problems/Problem17.hpp b/Headers/Problems/Problem17.hpp index 7ab1da1..678bd15 100644 --- a/Headers/Problems/Problem17.hpp +++ b/Headers/Problems/Problem17.hpp @@ -41,6 +41,7 @@ private: //Functions std::string makeWordFromNum(int number); //This function makes a word out of the number passed into it + std::string wordHelper(int num); //This function helps makeWordFromNum() by returning the words for the numbers 1-9 uint64_t countLetters(std::string str); //This counts the number of letters in the string that is passed in (ignoring numbers and punctuation) public: //Constructor diff --git a/src/Problems/Problem17.cpp b/src/Problems/Problem17.cpp index e3ad750..a0782f1 100644 --- a/src/Problems/Problem17.cpp +++ b/src/Problems/Problem17.cpp @@ -34,7 +34,7 @@ int Problem17::START_NUM = 1; int Problem17::STOP_NUM = 1000; //This function makes a word out of the number passed into it -std::string Problem17::makeWord(int num){ +std::string Problem17::makeWordFromNum(int num){ int currentDivider = 1000; int currentNum; bool teen = false; @@ -154,8 +154,8 @@ std::string Problem17::wordHelper(int num){ case 3: tempString += "three"; break; case 2: tempString += "two"; break; case 1: tempString += "one"; break; - //TODO: Throw an exception - default: tempString += "ERROR"; break; + case 0: tempString += "zero"; break; + default: throw Unsolved("Number passed to wordHelper was either > 10 or < 0"); break; } return tempString; } @@ -187,7 +187,7 @@ void Problem17::solve(){ //Step through every element in nums and get the word representations of the numbers for(int cnt = START_NUM;cnt <= STOP_NUM;++cnt){ - std::string words = makeWord(cnt); //Get the words of each number in turn + std::string words = makeWordFromNum(cnt); //Get the words of each number in turn letterCount += countLetters(words); //Add the number of letters to the running tally }