Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.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++ - Fatal编程技术网

C++ 用于加密字符串的函数不';行不通

C++ 用于加密字符串的函数不';行不通,c++,C++,我创建了这个函数来加密字符串,但它不起作用 软件搜索主字符串“s”中的每个字符,只需找到它(在字符串z1和z2中)就可以用前面三个位置的字符替换它 #include <iostream> #include <string> using namespace std; string change(string& inp) { string z1 = {"abcdefghilmnopqrtsvzabc"}; string z2 = {"ABCDEFG

我创建了这个函数来加密字符串,但它不起作用

软件搜索主字符串“s”中的每个字符,只需找到它(在字符串z1和z2中)就可以用前面三个位置的字符替换它

#include <iostream>
#include <string>
using namespace std;

string change(string& inp)
{

    string z1 = {"abcdefghilmnopqrtsvzabc"};
    string z2 = {"ABCDEFGHILMNOPQRSTUVZABC"};

    for(char c : inp)
    {
        if(z1.find(c) != string::npos) //string::npos is the "false" member 
        {
            auto x = z1.find(c);       //returned by **z1.find(c)**
            c = z1[x + 3];
            // The software searches each character, just find it (in strings z1 and z2) 
            // replaces it with the character in three positions more ahead
        }
        else
        {
            auto y = z2.find(c);
            c = z2[y + 3];
        }
    }
    return inp;
}

int main()
{
    string s = {"abcd"};

    cout << change(s) << endl;
}
#包括
#包括
使用名称空间std;
字符串更改(字符串和inp)
{
字符串z1={“abcdefghilmnopqrtsvzabc”};
字符串z2={“ABCDEFGHILMNOPQRSTUVZABC”};
用于(字符c:inp)
{
if(z1.find(c)!=string::npos)//string::npos是“false”成员
{
auto x=z1.find(c);//由**z1.find(c)返回**
c=z1[x+3];
//软件搜索每个字符,只需找到它(在字符串z1和z2中)
//将其替换为前面三个位置的角色
}
其他的
{
自动y=z2。查找(c);
c=z2[y+3];
}
}
返回inp;
}
int main()
{
字符串s={“abcd”};

如果您的
for
循环没有得到对字符串中字符的引用,您会得到一个带有字符值的变量
c
。操纵
c
不会更改给定字符串
inp
。请将循环从c++11样式更改回:

for(int i=0;i<inp.length();i++ ){                       
    if(z1.find(inp[i])!=string::npos){       //string::npos is the "false" member 
        auto x =z1.find(inp[i]);             //returned by **z1.find(c)**
        inp[i]=z1[x+3];                      
                                         //The software searches each character, just find it (in strings z1 and z2) 
    }                                    //replaces it with the character in three positions more ahead
    else                                
    {
        auto y=z2.find(inp[i]);
        inp[i]=z2[y+3];
    }
}

我错过了这个。

用调试器完成。我不知道如何使用它。我学习C++ 2个月。在大学里,教授没有告诉我们如何使用。it@themagiciant95现在是学习的时候了。如果您使用的是IDE,它通常包括单击行的左侧设置断点,然后选择使用调试器并单击点击显示步骤的按钮。我保证5-10分钟的查看时间可以解释任何IDE的基本用法。一般来说,命令行有点复杂,但如果您学习了所需的两个基本命令,就不会太糟糕了。@themagiciant95如果您没有调试器的访问权限,您总是可以
cout
信息来帮助您非常感谢,这是一个非常愚蠢的错误:)。只需在for(char c:inp)中添加一个“&”,现在就可以了works@themagiciant95不管怎么说,只要两个月的C++就不坏了。当然,还有改进的空间;
for(char &c:inp){