【发布时间】:2020-06-17 14:46:41
【问题描述】:
我最近开始使用 C++,我想创建一个可以调用的简单的终止密码函数,但是因为我从我的 python 程序中复制了编码结构,我似乎收到了 4 个错误和 2 个警告。模式位是一个布尔值,其中 true 是加密,false 是解密(它在 python 上工作,所以嘿):)。
我创建“int”所在的函数的第一个,它说“标识符“in”未定义”
同一行中的第二个说“预期为')'”
第三个是在 3 个 if 语句之后,表示“标识符“CharPos”未定义”,即使它已定义
和 Forth 在同一行说“'CharPos':未声明的标识符”
#include <iostream>
#include <fstream>
#include <string>
std::string Encryption(std::string Password, int Key, bool Mode) {
std::string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
std::string EncryptPass = "";
if (Key > 36) {
Key = Key % 36;
}
for (int X = 0; X < Password.length(); X++) {
if (Password.at(X) == ' ') {
EncryptPass = EncryptPass + " ";
}
else {
for (int Y = 0; Y < 36; Y++) {
if (Password.at(X) == Alphabet.at(Y)) {
if (Mode == true) {
int CharPos = Y + Key;
if (CharPos > 35) {
CharPos = CharPos - 36;
}
}
if (Mode == false) {
int CharPos = Y - Key;
if (CharPos < 0) {
CharPos = CharPos + 36;
}
}
if (Mode != true and Mode != false) {
int CharPos = 0;
}
char CharPos2 = CharPos;
char EncryptChar = Alphabet.at(CharPos2);
EncryptPass = EncryptPass + EncryptChar;
}
}
}
}
return EncryptPass;
}
任何帮助将不胜感激
【问题讨论】:
-
在有错误的每一行旁边加上注释,而不是描述错误的位置。此外,粘贴每个错误的完整错误消息。
-
int CharPos = 0;毫无意义。请记住,当您击中第一个 } 时,范围就会消失 -
char CharPos2 = CharPos;编译器是正确的CharPos在这一行没有定义。您在三个不同的范围内定义了 3 个CharPos变量 {} 已启用。 -
我很确定您的
Alphabet没有 94 个字符。 -
而
CharPos在循环结束时将始终为0,因为Mode != true or Mode != false始终为真。
标签: c++ caesar-cipher