错误:从 'char' 到 'const char' [-允许] strcat(加密,密钥[i])的转换无效;

error: invalid conversion from 'char' to 'const char' [-fpermissive] strcat(encrypted, key[i]);

本文关键字:char 密钥 无效 转换 加密 允许 const strcat 错误      更新时间:2023-10-16

为什么strcat((函数不起作用。错误是从"char"到"const char"的转换无效。为什么错误显示const字符。我没有将任何数组声明为const。

#include<iostream>
#include<cstring>
using namespace std;
int main(){
cout<<"Enter the secret message"<<endl;
char secret[80]{};//Initialising leads to removal of garbage values
cin.getline(secret,80);
char alphabet[] {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
char key[] {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
int i{};
char encrypted[80] {};
char decrypted[80] {};
for(auto val:secret){
i=0;
while(i<strlen(alphabet) && val!=alphabet[i]){
i++;
}
if(i<strlen(alphabet))
strcat(encrypted, key[i]);
else
strcat(encrypted, val);
}
cout<<"Encrypting..."<<endl;
cout<<"Encrypted value: "<<encrypted;
for(auto val: encrypted){
i=0;
while(i<strlen(encrypted) && val!=encrypted[i])
i++;
if(i<strlen(encrypted))
strcat(decrypted, alphabet[i]);
else
strcat(decrypted, val);
}
cout<<"Decrypting: "<<endl;
cout<<"Decrypted value: "<<decrypted<<endl;
return 0;
}

strcat函数将一个字符串连接到另一个字符串上。但是key[i]不是字符串。