Added propper error handling

This commit is contained in:
2019-03-07 12:38:22 -05:00
parent 80ebb77040
commit b33f790243
4 changed files with 91 additions and 23 deletions

View File

@@ -88,6 +88,10 @@ void Vigenere::setKeyword(std::string key){
//Make sure offset is empty before adding to it
offset.clear();
setOffset();
//If after all the eliminating of unusable characters the keyword is empty throw an error
if(keyword == ""){
throw emptyKeyword();
}
}
/**
@@ -157,11 +161,13 @@ std::string Vigenere::encode(){
*/
std::string Vigenere::encode(std::string key, std::string input){
reset();
setKeyword(key);
//Throw an error if there is no keyword
//Would be better to throw an error here
if(keyword == ""){
return "Error! Empty keyword\n";
try{
setKeyword(key);
}
//Catch the emptyKeyword error if it was thrown
catch(emptyKeyword){
std::string errorString = "After all unusable characters were stripped away the keyword given was empty";
return errorString;
}
setInputString(input);
return encode();
@@ -200,11 +206,13 @@ std::string Vigenere::decode(){
*/
std::string Vigenere::decode(std::string key, std::string input){
reset();
setKeyword(key);
//Throw an error if there is no keyword
//Would be better to throw an error here
if(keyword == ""){
return "Error! Empty keyword\n";
try{
setKeyword(key);
}
//Catch the emptyKeyword error if it was thrown
catch(emptyKeyword){
std::string errorString = "After all unusable characters were stripped away the keyword given was empty";
return errorString;
}
setInputString(input);
return decode();