【问题标题】:String input xor encryption program字符串输入异或加密程序
【发布时间】:2014-06-28 14:03:58
【问题描述】:

说明:
我正在尝试编写一个有趣的基本程序,该程序将输入一个字符串/短语,然后对它进行异或加密,最后计算出加密的短语。我正在使用 Mac,所以我将使用 xcode 编译并在终端中运行它。

问题:
我在输入可以异或加密的字符串时出错,请参见下面的代码:

代码:

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

int main ()
{
string mystr;
cout << "What's the phrase to be Encrypted? ";
//getline (cin, mystr);

char string[11]= getline (cin, mystr); //ERROR: Array must be initialized with brace-enclosed initializer
cout << "Phrase to be Encrypted: " << mystr << ".\n";
char key[11]="ABCDEFGHIJ";  //The Encryption Key, for now its generic
for(int x=0; x<10; x++)
{
    string[x]=mystr[11]^key[x];
    cout<<string[x];
}
return 0;
}

帮助:
请指出并解释或提供示例我收到上述错误代码的原因。

【问题讨论】:

    标签: c++ encryption xor


    【解决方案1】:

    您没有正确调用getline。当 string 已经定义为类型时,您还尝试将其用作变量。我会尝试更多类似的东西:

    getline(cin, mystr);
    
    string result;
    
    for (int i=0; i<10; i++) {
        result.push_back(mystr[i] ^ key[i]);
        cout << result[i];
    }
    

    【讨论】:

      【解决方案2】:

      getline 的返回类型是流,不能将其分配给 char 数组。要初始化一个数组,你必须使用:

      char s[10] = {'h', 'i', 0};
      

      或者是 char 数组的简写:

      char s[10] = "hi";
      

      在您的问题中,您可能希望使用注释掉的 getline 语句和

      const char* string = mystr.c_str();
      

      【讨论】:

        猜你喜欢
        • 2014-06-28
        • 1970-01-01
        • 1970-01-01
        • 2014-07-10
        • 1970-01-01
        • 1970-01-01
        • 2014-06-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多