c++ - 密码无效

标签 c++ compiler-errors

我最近开始学习c++,并且我一直在尝试创建解决方案来挑战自己,这些挑战之一是对文本进行加密并将其最终保存到文本文件中的密码。我当前使用的代码无法编译,因为它无法识别replace语句,这是到目前为止的代码。

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <string>

int main()
{
// declare all variables
std::string text;
std::string s;
std::string uncipheredText;

//Introducing the program
std::cout << "Welcome to Cipher." << std::endl;

//Asks the user to input the text they want to encrypt and saves it to the unciphered text variable.
std::cout << "Enter the text you want to cipher" << std::endl;
std::cin >> s;

//replaces all characters in the variable "s"
std::replace(s.begin(), s.end(), 'Q', 'M');
std::replace(s.begin(), s.end(), 'E', 'B');
std::replace(s.begin(), s.end(), 'T', 'C');
std::replace(s.begin(), s.end(), 'U', 'Z');
std::replace(s.begin(), s.end(), 'O', 'S');
std::replace(s.begin(), s.end(), 'L', 'F');
std::replace(s.begin(), s.end(), 'J', 'H');
std::replace(s.begin(), s.end(), 'G', 'K');
std::replace(s.begin(), s.end(), 'D', 'P');
std::replace(s.begin(), s.end(), 'A', 'I');
std::replace(s.begin(), s.end(), 'X', 'Y');
std::replace(s.begin(), s.end(), 'V', 'R');
std::replace(s.begin(), s.end(), 'N', 'W');
std::replace(s.begin(), s.end(), ' ', '#');


s = text;

std::cout << "Encrypted Text: " << text << std::endl;

//replaces all characters in the variable "s"
std::replace(s.begin(), s.end(), 'Q', 'M');
std::replace(s.begin(), s.end(), 'E', 'B');
std::replace(s.begin(), s.end(), 'T', 'C');
std::replace(s.begin(), s.end(), 'U', 'Z');
std::replace(s.begin(), s.end(), 'O', 'S');
std::replace(s.begin(), s.end(), 'L', 'F');
std::replace(s.begin(), s.end(), 'J', 'H');
std::replace(s.begin(), s.end(), 'G', 'K');
std::replace(s.begin(), s.end(), 'D', 'P');
std::replace(s.begin(), s.end(), 'A', 'I');
std::replace(s.begin(), s.end(), 'X', 'Y');
std::replace(s.begin(), s.end(), 'V', 'R');
std::replace(s.begin(), s.end(), 'N', 'W');
std::replace(s.begin(), s.end(), ' ', '#');

s = uncipheredText;

std::cout << "Decrypted Text: " << uncipheredText << std::endl;

/*
ofstream myfile;
myfile.open("dump.txt");
myfile << text;
myfile.close();
*/

return 0;
}   

最佳答案

如@AlgirdasPreidžius的评论中所述,您需要包括算法库。另外,您重复了以下代码:

//replaces all characters in the variable "s"
std::replace(s.begin(), s.end(), 'Q', 'M');
std::replace(s.begin(), s.end(), 'E', 'B');
std::replace(s.begin(), s.end(), 'T', 'C');
std::replace(s.begin(), s.end(), 'U', 'Z');
std::replace(s.begin(), s.end(), 'O', 'S');
std::replace(s.begin(), s.end(), 'L', 'F');
std::replace(s.begin(), s.end(), 'J', 'H');
std::replace(s.begin(), s.end(), 'G', 'K');
std::replace(s.begin(), s.end(), 'D', 'P');
std::replace(s.begin(), s.end(), 'A', 'I');
std::replace(s.begin(), s.end(), 'X', 'Y');
std::replace(s.begin(), s.end(), 'V', 'R');
std::replace(s.begin(), s.end(), 'N', 'W');
std::replace(s.begin(), s.end(), ' ', '#');

关于c++ - 密码无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45464216/

相关文章:

c++ - 解决模板友元功能的问题

c++ - 使用g++编译器在Visual Studio中编译C++程序会在最后生成%

c++ - expat 对 `XML_SetElementHandler' 的 undefined reference

c++ - 列表容器 - 使用不相关的类型存储和移除

c++ - 编译 C++ 代码时 undefined reference 构造函数

c - 你如何在 C 中定义一个不透明的结构数组?

class - Scala REPL "error: value > is not a member of type parameter T"

c++ - 使用不同文件声明函数/vector 的 C++ 中的多重定义错误

java - OOP 和私有(private)字段的继承

c++ - 如何从 "this"对象本身内部将shared_ptr返回到当前对象