Anchara
Anchara

Reputation: 33

Logic Error when trying to output a decrypted string from an encrypted string in Visual Studios C++

my program takes a encrypted string into a class function EncryptedString(string str). It seems like everything is being outputted correctly aside from when I try to call a get function to get the decrypted string I am met with "ZZZZZZZZ". The Program takes a phrase or sentence, encrypts the sentence and deletes any illegal characters, then decrypts it and outputs the decrypted result. I am putting in "Hello World!" and it deletes the ! just fine. The space is supposed to kept however it too is also turned into a Z.

I am also having a problem with the output of my encryption. I am also to output the encrypted version of the phrase. However when I output it, nothing is being put out.

Here is the code for the entire EncryptedString.cpp file. Thank you to whoever helps me with this problem and if you need to see the main.cpp file or the header file for that declares the functions I will be happy to provide, it is just I do not think they are not necessary with this error. However I could be wrong.

#include "EncryptedString.h"


string decrypted;

EncryptedString::EncryptedString(string str)
{
    string enCrypt = str;
    set(enCrypt);

}



void EncryptedString::set(string str)
{
    char chBase = 'A';


    string enCry = str;
    for (int i = 0; i < enCry.length(); i++)
    {
        char ch = enCry[i];

        if ((enCry[i] < chBase || (enCry[i] > chBase + 25 && enCry[i] <  tolower(chBase)) || enCry[i] > tolower(chBase + 25)) && enCry[i] != ' ')
        {
            enCry.erase(enCry.begin() + i);
        }
        else
        {
            if (enCry[i] = chBase + 25)
            {
                enCry[i] = 'A';
            }
            else if (enCry[i] = tolower(chBase) + 25)
            {
                enCry[i] = 'a';
            }
            else if (enCry[i] = ' ')
            {
                enCry[i] = ' ';
            }
            else
            {
                enCry[i] = ch + 1;
            }

        }


    }

    EncryptedString::encryption == enCry;
    string decrypt = enCry;

    for (int i = 0; i < decrypt.length(); i++)
    {
        char ch = decrypt[i];

        if (decrypt[i] = 'A')
        {
            decrypt[i] = 'Z';
        }
        else if (decrypt[i] = 'a')
        {
            decrypt[i] = 'z';
        }
        else if (decrypt[i] = ' ')
        {
            decrypt[i] = ' ';
        }
        else
        {
            decrypt[i] = ch - 1;
        }
    }

    decrypted = decrypt;
}

const string EncryptedString::get()
{

    return decrypted;
}

const string EncryptedString::getEncrypted()
{
    return EncryptedString::encryption;
}

Upvotes: 1

Views: 54

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

The issue is that you're using the wrong operator for equality comparisons:

if (enCry[i] = chBase + 25)

In C++, you use == to compare for equality, not =. The above line should be:

if (enCry[i] == chBase + 25)

You make similar errors in several other lines in your program. Correct those errors and rerun your program.

Upvotes: 1

Related Questions