From c8c56ef1595f08fe916c809bd958f22e724d732c Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 14 May 2018 13:46:37 -0400 Subject: [PATCH] Updated to fix errors with not clearing strings during encoding/decoding --- Headers/Autokey.hpp | 4 ++-- SourceFiles/Autokey.cpp | 7 ++++--- SourceFiles/Caesar.cpp | 2 ++ SourceFiles/Vigenere.cpp | 2 ++ 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Headers/Autokey.hpp b/Headers/Autokey.hpp index 16a7c03..774b8a4 100644 --- a/Headers/Autokey.hpp +++ b/Headers/Autokey.hpp @@ -14,8 +14,8 @@ //TODO: Might be able to make encoding and decoding faster for longer messages by actually using a grid of alphabets (Caesar Ciphers) class Autokey : public Vigenere{ protected: - void encodeSet(std::string& key, std::string input); //Special rules for setting the strings for encoding - void decodeSet(std::string& key, std::string input); //Setting the strings for decoding + void encodeSet(std::string key, std::string input); //Special rules for setting the strings for encoding + void decodeSet(std::string key, std::string input); //Setting the strings for decoding std::string decode(); //Decodes the inputString public: Autokey(); diff --git a/SourceFiles/Autokey.cpp b/SourceFiles/Autokey.cpp index 2b2b605..888db44 100644 --- a/SourceFiles/Autokey.cpp +++ b/SourceFiles/Autokey.cpp @@ -30,7 +30,7 @@ Autokey::~Autokey(){ * @param key The keyword used for the cipher * @param input The string that you wish to encode */ -void Autokey::encodeSet(std::string& key, std::string input){ +void Autokey::encodeSet(std::string key, std::string input){ //Remove all unneccessary elements from the key Vigenere::setKeyword(key); key = Vigenere::getKeyword(); @@ -57,10 +57,11 @@ void Autokey::encodeSet(std::string& key, std::string input){ * @param key The keyword used for the cipher * @param input The string that you wish to decode */ -void Autokey::decodeSet(std::string& key, std::string input){ +void Autokey::decodeSet(std::string key, std::string input){ //Remove all unneccessary elements from the key Vigenere::setKeyword(key); //Remove all unneccessary elements from the input + inputString = ""; Vigenere::setInputString(input); } @@ -133,4 +134,4 @@ std::string Autokey::decode(std::string key, std::string input){ setInputString(input); decodeSet(key, input); //Decoding is a bit different because part of the key is also part of the original message return Autokey::decode(); -} +} \ No newline at end of file diff --git a/SourceFiles/Caesar.cpp b/SourceFiles/Caesar.cpp index 460db88..bde50f0 100644 --- a/SourceFiles/Caesar.cpp +++ b/SourceFiles/Caesar.cpp @@ -118,6 +118,7 @@ std::string Caesar::encode(){ * @return The encoded message */ std::string Caesar::encode(int shiftAmount, std::string input){ + reset(); setShift(shiftAmount); setInputString(input); return encode(); @@ -167,6 +168,7 @@ std::string Caesar::decode(){ * @return The decoded message */ std::string Caesar::decode(int shiftAmount, std::string input){ + reset(); setShift(shiftAmount); setInputString(input); return decode(); diff --git a/SourceFiles/Vigenere.cpp b/SourceFiles/Vigenere.cpp index 9d2b7e7..9080e58 100644 --- a/SourceFiles/Vigenere.cpp +++ b/SourceFiles/Vigenere.cpp @@ -123,6 +123,7 @@ std::vector Vigenere::getOffsets() const{ * @return The encoded message */ std::string Vigenere::encode(){ + outputString = ""; //Reserve the correct size for the output string to increase speed for longer messages outputString.reserve(inputString.size()); @@ -167,6 +168,7 @@ std::string Vigenere::encode(std::string key, std::string input){ * @return The decoded message */ std::string Vigenere::decode(){ + outputString = ""; //Reserve the correct size for the output string to increase speed for longer messages outputString.reserve(inputString.size());