Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从加密字符串程序获取输出时出现逻辑错误。_C++_Visual Studio_Encryption_Logic - Fatal编程技术网

C++ 从加密字符串程序获取输出时出现逻辑错误。

C++ 从加密字符串程序获取输出时出现逻辑错误。,c++,visual-studio,encryption,logic,C++,Visual Studio,Encryption,Logic,我制作了一个程序,将一个字符串放入EncryptedString类中,然后对所述字符串进行加密,删除任何不是空格或小写或大写字母的内容。在我输入一个类似496496@##########!的字符串之前,一切似乎都很正常!!!4然后删除一些,保留其他。我有一些应该输出的示例 输入:你好,世界 预期解密:你好,世界 应加密:Ifmmp Xpsme 输出解密:Hello World 加密输出:Ifmmp Xpsme 你好,世界工作,只是罚款和删除 然而,当我试着做“一个苹果在秋天跑掉了”时,我明白

我制作了一个程序,将一个字符串放入EncryptedString类中,然后对所述字符串进行加密,删除任何不是空格或小写或大写字母的内容。在我输入一个类似496496@##########!的字符串之前,一切似乎都很正常!!!4然后删除一些,保留其他。我有一些应该输出的示例

  • 输入:你好,世界
  • 预期解密:你好,世界
  • 应加密:Ifmmp Xpsme
  • 输出解密:Hello World
  • 加密输出:Ifmmp Xpsme
你好,世界工作,只是罚款和删除

然而,当我试着做“一个苹果在秋天跑掉了”时,我明白了

  • 输入:一个苹果在秋天跑掉了z!!14
  • 预期解密:一个苹果在秋天逃跑了
  • 预期加密:B bqqmf sbo bxbz jo bvuvno a
  • 输出解密:一个苹果在秋天的Z3跑掉了
  • 输出加密:B bqqmf sbo bxbz jo bvuvno a!4
这里还有一个例子

  • 输入:Emily Dickinson 1152163!!!@@@@@@@@@
  • 预期解密:艾米丽·迪金森
  • 应加密:Fnjmz Ejeljotpo
  • 输出解密:Emily Dickinson 015
  • 输出加密:Fnjmz Ejdljotpo126!!@
我想可能是当我在decrypt.length()和enCry.length()上迭代代码时它正在检查元素?但是,我感觉不是这样,因为它可以删除其他数字和符号,但出于某种原因,一些保留下来。在我的迭代过程中,我下面的代码是否有什么问题导致了这种情况

    //This function takes the phrase,word or sentence and encrypts it, removing any illegal characters aside from ' ' and then proceeds to decrypt it then output them to the get functions. 
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] != ' ')
        {
            enCry.erase(enCry.begin() + i);
        }
        else if (enCry[i] > chBase + 25 && enCry[i] < tolower(chBase) && enCry[i] != ' ')
        {
            enCry.erase(enCry.begin() + i);
        }
        else if (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;
}


//This function outputs the decryption after the phrase was encrypted. 
const string EncryptedString::get()
{

    return decrypted;
}   
//This function outputs the encryption of the phrase. 
const string EncryptedString::getEncrypted()
{
    return EncryptedString::encryption;
}
//此函数获取短语、单词或句子并对其进行加密,删除除“”之外的任何非法字符,然后对其进行解密,然后将其输出到get函数。
void EncryptedString::set(字符串str)
{
char chBase='A';
字符串enCry=str;
对于(int i=0;ichBase+25和&enCry[i]tolower(chBase+25)和&enCry[i]!='')
{
enCry.erase(enCry.begin()+i);
}
其他的
{
如果(enCry[i]==chBase+25)
{
enCry[i]=“A”;
}
否则,如果(enCry[i]==tolower(chBase)+25)
{
enCry[i]=“a”;
}
else if(enCry[i]='')
{
enCry[i]='';
}
其他的
{ 
enCry[i]=ch+1;
}
}
}
EncryptedString::encryption=enCry;
字符串解密=enCry;
for(int i=0;i
这里的更多信息是我使用这些函数的main.cpp中的内容。我想可能是因为我在test2中设置了两次,但我通过向hello world添加数字来测试test1,输出只是保留了一些数字。如果您需要查看该示例,我将提供

#include "EncryptedString.h"
#include <windows.h>

int main()
{
    cout << "TEST 1" << endl << endl;
    EncryptedString test1("Hello World!");
    cout << test1.get();
    cout << endl << endl;
    cout << test1.getEncrypted();

    cout << endl << endl << "TEST 2" << endl << endl;


    EncryptedString test2;
    test2.set("A apple ran away in autumn z!!14?");
    cout << endl << endl;
    cout << test2.get();
    cout << endl << endl;
    cout << test2.getEncrypted();

    cout << endl << endl; 
    test2.set("Emily Dickson1152163!!!@@#@#!!!");
    cout << test2.get() << endl << endl << test2.getEncrypted();

    //being used for me to see the output. 
    Sleep(15000);
}
#包括“EncryptedString.h”
#包括
int main()
{

如果有人能看出我哪里出错了--使用编译器附带的调试器单步执行程序。
str.erase(std::remove_If(str.begin(),str.end(),[](char ch){return!isalpha(ch)&&&!isspace(ch)},str end());
--这一行删除了除空格以外的所有非法字符。简而言之,删除了为执行此操作而编写的整个代码块。需要编写的代码越少,犯的错误就越少。我正在使用调试器,并成功修复了两个与符号不匹配有关的警告。将for(int I…)更改为for(尺码……)。但是,仍然得到相同的结果。是否有其他方法可以调试程序以解决手头的问题,还是只需要查看我忽略的内容?我猜您没有使用调试器,但您认为您使用了调试器。调试器允许您一次运行一条语句。您可以检查变量、设置中断点,查看程序是否按照您设计的流程运行,等等。您所描述的是编译器作为警告提供给您的内容。一旦构建了程序,调试器允许您运行您的程序--无需考虑您的程序正在做什么,您实际上可以一步一步地运行它,并观察它正在做什么。此外,修订:
str.erase(std::remove_if(str.begin(),str.end(),[](char ch){return(!isalpha(ch)&&&&!isspace(ch))| | isdigit(ch)},str.end())
--这也会删除数字。问题似乎是您未能删除所有数字,但您需要查看当进入第二个
for
循环时,您的字符串是否实际包含有效字符。您可以简单地作为调试助手输出您在该点尝试解密的内容,即使您没有这样做知道如何使用调试器。