From f18ae195524cb07dcafe047041a92087367ef9d1 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Sun, 13 May 2018 23:09:30 -0400 Subject: [PATCH] Fixed bug when attempting to encode/decode with a blank keyword --- SourceFiles/Vigenere.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/SourceFiles/Vigenere.cpp b/SourceFiles/Vigenere.cpp index 5bc3f8f..9d2b7e7 100644 --- a/SourceFiles/Vigenere.cpp +++ b/SourceFiles/Vigenere.cpp @@ -31,9 +31,6 @@ Vigenere::~Vigenere(){ * @param input The string you want to encode/decode */ void Vigenere::setInputString(std::string input){ - //Make sure inputString is clear - inputString = ""; - //Loop through every character in input. Remove all whitespace and punctuation and make sure all letters are capital and add it to inputString for(unsigned int cnt = 0;cnt < input.size();++cnt){ char letter = input[cnt]; @@ -71,8 +68,6 @@ std::string Vigenere::getOutputString() const{ * @param key The keyword used for the cipher */ void Vigenere::setKeyword(std::string key){ - //Make sure the keyword is blank - keyword = ""; //Loop through every letter in the key and make sure all of them are uppercase letters for(unsigned int cnt = 0;cnt < key.size();++cnt){ char letter = key[cnt]; @@ -128,8 +123,6 @@ std::vector Vigenere::getOffsets() const{ * @return The encoded message */ std::string Vigenere::encode(){ - //Clear the outputString - outputString = ""; //Reserve the correct size for the output string to increase speed for longer messages outputString.reserve(inputString.size()); @@ -157,7 +150,13 @@ std::string Vigenere::encode(){ * @return The encoded message */ 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"; + } setInputString(input); return encode(); } @@ -168,8 +167,6 @@ std::string Vigenere::encode(std::string key, std::string input){ * @return The decoded message */ std::string Vigenere::decode(){ - //Clear the outputString - outputString = ""; //Reserve the correct size for the output string to increase speed for longer messages outputString.reserve(inputString.size()); @@ -196,7 +193,13 @@ std::string Vigenere::decode(){ * @return The decoded message */ 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"; + } setInputString(input); return decode(); }