2017-08-02 60 views
-5

我最近开始学习C++,并且一直在尝试创建解决方案来挑战自己,其中一个挑战是加密文本并最终将其保存到文本文件的密码。我目前使用的代码不会编译,因为它不会识别替换语句,这是我的代码。密码不会工作

#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; 
} 
+5

你看过['std :: replace'](http://en.cppreference.com/w/cpp/algorithm/replace)的文档吗?如果你这样做,你会知道,它在''中定义。你包括它吗? –

+2

s = text在这一点上,“text”不是一个空字符串吗? – systemcpro

+1

你写作业的方式是错误的。 – molbdnilo

回答

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(), ' ', '#');