Updated 17 to throw exception

This commit is contained in:
2020-08-28 15:13:13 -04:00
parent 252a6c9c00
commit 000d6da85f
2 changed files with 5 additions and 4 deletions

View File

@@ -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

View File

@@ -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
}