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
*/
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<unsigned int> 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();
}