问题描述
描述:
我试图写一个乐趣的基本程序,将输入一个字符串/短语,然后xor加密它,最后cout加密短语。我正在mac上工作,所以我将编译xcode和运行它在终端。
Description:
I am trying to write a basic program for fun that will input a string/phrase then xor encrypt it and in the end cout the encrypted phrase. I am working on a mac so I will be compiling with xcode and running it in terminal.
问题:
输入可以进行加密的字符串时出错,请参阅下面的代码:
Problem:
I am having an error with inputting a string that can be xor encrypted, see code below:
代码:
#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;
}
帮助:
请识别并说明或提供示例为什么我收到上面的错误代码。
Help:
please identify and explain or provide examples why I am receiving the error code above.
推荐答案
您不是调用 getline
正确。你还试图使用 string
作为一个变量,当它已经被定义为一个类型。我会尝试更像这样:
You're not calling getline
correctly. You're also trying to use string
as a variable when it's already defined as a type. I'd try something more like this:
getline(cin, mystr);
string result;
for (int i=0; i<10; i++) {
result.push_back(mystr[i] ^ key[i]);
cout << result[i];
}
这篇关于字符串输入xor加密程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!