Fixed bug when attempting to encode/decode with a blank keyword

This commit is contained in:
2018-05-13 23:09:30 -04:00
parent 90dcd3412c
commit f18ae19552

View File

@@ -31,9 +31,6 @@ Vigenere::~Vigenere(){
* @param input The string you want to encode/decode * @param input The string you want to encode/decode
*/ */
void Vigenere::setInputString(std::string input){ 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 //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){ for(unsigned int cnt = 0;cnt < input.size();++cnt){
char letter = input[cnt]; char letter = input[cnt];
@@ -71,8 +68,6 @@ std::string Vigenere::getOutputString() const{
* @param key The keyword used for the cipher * @param key The keyword used for the cipher
*/ */
void Vigenere::setKeyword(std::string key){ 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 //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){ for(unsigned int cnt = 0;cnt < key.size();++cnt){
char letter = key[cnt]; char letter = key[cnt];
@@ -128,8 +123,6 @@ std::vector<unsigned int> Vigenere::getOffsets() const{
* @return The encoded message * @return The encoded message
*/ */
std::string Vigenere::encode(){ std::string Vigenere::encode(){
//Clear the outputString
outputString = "";
//Reserve the correct size for the output string to increase speed for longer messages //Reserve the correct size for the output string to increase speed for longer messages
outputString.reserve(inputString.size()); outputString.reserve(inputString.size());
@@ -157,7 +150,13 @@ std::string Vigenere::encode(){
* @return The encoded message * @return The encoded message
*/ */
std::string Vigenere::encode(std::string key, std::string input){ std::string Vigenere::encode(std::string key, std::string input){
reset();
setKeyword(key); 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); setInputString(input);
return encode(); return encode();
} }
@@ -168,8 +167,6 @@ std::string Vigenere::encode(std::string key, std::string input){
* @return The decoded message * @return The decoded message
*/ */
std::string Vigenere::decode(){ std::string Vigenere::decode(){
//Clear the outputString
outputString = "";
//Reserve the correct size for the output string to increase speed for longer messages //Reserve the correct size for the output string to increase speed for longer messages
outputString.reserve(inputString.size()); outputString.reserve(inputString.size());
@@ -196,7 +193,13 @@ std::string Vigenere::decode(){
* @return The decoded message * @return The decoded message
*/ */
std::string Vigenere::decode(std::string key, std::string input){ std::string Vigenere::decode(std::string key, std::string input){
reset();
setKeyword(key); 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); setInputString(input);
return decode(); return decode();
} }