2017-08-02 4 views
-5

Ich begann C++ zu lernen vor kurzem, und ich habe versucht, Lösungen zu schaffen, mich selbst herauszufordern, eine dieser Herausforderungen war eine Chiffre, die Text verschlüsselt und schließlich speichert sie in einer Textdatei. Der Code, den ich gerade verwende, wird nicht kompiliert, weil er die Ersetzungsanweisung nicht erkennt, das ist bisher mein Code.Cipher-Code wird nicht funktionieren

#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

Sah Sie in der Dokumentation von [ 'std :: replace'] (http://en.cppreference.com/w/cpp/algorithm/replace)? Wenn Sie dies tun, würden Sie wissen, dass es in '' definiert ist. Hast du es aufgenommen? –

+2

s = Text Ist "Text" an dieser Stelle nicht einfach ein leerer String? – systemcpro

+1

Sie schreiben Aufgaben falsch herum. – molbdnilo

Antwort

0

Wie in den Kommentaren von @Algirdas Preidžius erwähnt, müssen Sie eine Algorithmusbibliothek einschließen. Auch dupliziert Sie diesen Code:

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