c++ - 错误:从 'char' 到 'const char' 的无效转换 [-fpermissive] strcat(encrypted, key[i]);
问题描述
为什么 strcat() 函数不起作用。错误是从 'char' 到 'const char' 的无效转换。为什么错误显示 const char。我没有将任何数组声明为 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]
不是字符串。
推荐阅读
- java - 如何找到运行时 int 转换错误的修复程序?
- swift - 在 XCode Simulator 中启动具有位置服务使用权限的应用程序
- javascript - 下拉按钮的地图,检查是否点击了id
- sql - SQL,更新命令未正确结束
- json.net - JToken.ReadFrom 与 JToken.Load?
- javascript - 如果我的道具在 React 中发生变化,如何更新状态以强制重新渲染?
- react-native - 在 useEffect 中使用 useState
- git - 是否可以比较 git 中文件的暂存版本和隐藏版本?
- java - 抖动算法
- javascript - 我在我的 package.json 文件中放了什么来让“yarn start”工作?