2014-11-06 103 views
-2

我对C++非常新颖而且可怕。 我希望有人能够弄清楚这个错误的含义,我真的不知道该怎么做,我不是要求你让它变得更有活力,我意识到我的尴尬。C++简单的数字加密字母

我想把字母转换成数字,我知道这是弱加密。

代码:

#include <iostream> 
#include <cstdlib> 
#include <cstdio> 
#include <algorithm> 
#include <string> 

using namespace std; 

int main() 
{ 
std::string ptxt = ""; 
string etxt = ""; 
cin >> ptxt; 
std::replace(ptxt.begin(), ptxt.end(), 'a', '1'); 
std::replace(ptxt.begin(), ptxt.end(), 'b', '2'); 
std::replace(ptxt.begin(), ptxt.end(), 'c', '3'); 
std::replace(ptxt.begin(), ptxt.end(), 'd', '4'); 
std::replace(ptxt.begin(), ptxt.end(), 'e', '5'); 
std::replace(ptxt.begin(), ptxt.end(), 'f', '6'); 
std::replace(ptxt.begin(), ptxt.end(), 'g', '7'); 
std::replace(ptxt.begin(), ptxt.end(), 'h', '8'); 
std::replace(ptxt.begin(), ptxt.end(), 'i', '9'); 
std::replace(ptxt.begin(), ptxt.end(), 'j', '10'); 
std::replace(ptxt.begin(), ptxt.end(), 'k', '11'); 
std::replace(ptxt.begin(), ptxt.end(), 'l', '12'); 
std::replace(ptxt.begin(), ptxt.end(), 'm', '13'); 
std::replace(ptxt.begin(), ptxt.end(), 'n', '14'); 
std::replace(ptxt.begin(), ptxt.end(), 'o', '15'); 
std::replace(ptxt.begin(), ptxt.end(), 'p', '16'); 
std::replace(ptxt.begin(), ptxt.end(), 'q', '17'); 
std::replace(ptxt.begin(), ptxt.end(), 'r', '18'); 
std::replace(ptxt.begin(), ptxt.end(), 's', '19'); 
std::replace(ptxt.begin(), ptxt.end(), 't', '20'); 
std::replace(ptxt.begin(), ptxt.end(), 'u', '21'); 
std::replace(ptxt.begin(), ptxt.end(), 'v', '22'); 
std::replace(ptxt.begin(), ptxt.end(), 'w', '23'); 
std::replace(ptxt.begin(), ptxt.end(), 'x', '24'); 
std::replace(ptxt.begin(), ptxt.end(), 'y', '25'); 
std::replace(ptxt.begin(), ptxt.end(), 'z', '26'); 
return 0; 
} 

Build Log (Debug?)

+2

您需要查看警告。 – 2014-11-06 01:55:45

+1

你有没有读错误? ''10'不是单个字符。 – user657267 2014-11-06 01:56:33

+0

看起来很古怪,哇! – 2014-11-06 01:58:30

回答

3

让我们从构建日志的开头开始:

main.cpp|25|warning: multi-character character constant [-Wmultichar]| 

字符常量是像'a''1'。如果我们去看看代码此警告点,我们看到:

std::replace(ptxt.begin(), ptxt.end(), 'l', '12'); 

注意'12':有一些字符常量内的两个字符。这是警告你关于你有多个事实的事实。 “多字符”字符常量究竟是什么以及它们的优点并不重要,您不需要它们,也不应该使用它们。

如果你真的想像“你好”这样的文字编码为“85121215”或“8 5 12 12 15”,那么你将不得不重新思考你如何做替换。这是由于两个原因:在输出文件中,像'12'这样的多字符字符常量看起来不会像12,其次,因为std::replace不会处理用多个字符替换单个字符,如l

现在可以取代你所得到的单字符版本的MULT个字符的字符常量:

std::replace(ptxt.begin(), ptxt.end(), 'a', '1'); 
std::replace(ptxt.begin(), ptxt.end(), 'b', '2'); 
std::replace(ptxt.begin(), ptxt.end(), 'c', '3'); 
std::replace(ptxt.begin(), ptxt.end(), 'd', '4'); 
std::replace(ptxt.begin(), ptxt.end(), 'e', '5'); 
std::replace(ptxt.begin(), ptxt.end(), 'f', '6'); 
std::replace(ptxt.begin(), ptxt.end(), 'g', '7'); 
std::replace(ptxt.begin(), ptxt.end(), 'h', '8'); 
std::replace(ptxt.begin(), ptxt.end(), 'i', '9'); 
std::replace(ptxt.begin(), ptxt.end(), 'j', 'a'); 
std::replace(ptxt.begin(), ptxt.end(), 'k', 'b'); 
std::replace(ptxt.begin(), ptxt.end(), 'l', 'c'); 
std::replace(ptxt.begin(), ptxt.end(), 'm', 'd'); 
std::replace(ptxt.begin(), ptxt.end(), 'n', 'e'); 
std::replace(ptxt.begin(), ptxt.end(), 'o', 'f'); 
std::replace(ptxt.begin(), ptxt.end(), 'p', 'g'); 
std::replace(ptxt.begin(), ptxt.end(), 'q', 'h'); 
std::replace(ptxt.begin(), ptxt.end(), 'r', 'i'); 
std::replace(ptxt.begin(), ptxt.end(), 's', 'j'); 
std::replace(ptxt.begin(), ptxt.end(), 't', 'k'); 
std::replace(ptxt.begin(), ptxt.end(), 'u', 'l'); 
std::replace(ptxt.begin(), ptxt.end(), 'v', 'm'); 
std::replace(ptxt.begin(), ptxt.end(), 'w', 'n'); 
std::replace(ptxt.begin(), ptxt.end(), 'x', 'o'); 
std::replace(ptxt.begin(), ptxt.end(), 'y', 'p'); 
std::replace(ptxt.begin(), ptxt.end(), 'z', 'q'); 

这一更改解决在构建日志中的所有警告,并且也恰好解决所有错误也是如此。

如果你很好奇这些错误是什么:它们只是一种详细的方式告诉你,你的论点std::replace()是不正确的。当倒数第二个参数是char时,多字符碰巧具有类型int而不是char,并且std::replace()不接受int作为最后一个参数。

          this has type `int` 
               | 
               V 
std::replace(ptxt.begin(), ptxt.end(), 'l', '12'); 
             ^
             | 
            this has type `char`... mismatch error between `int` and `char` 

  • 以在一个时间问题之一是为了避免被所有的人一下子不知所措的好方法。从一开始就一次只拿一个。通常修复之前报告的一个问题会导致其他警告和错误消失,因此您不必为此烦恼。

  • 不要忽视警告。即使你的程序可以在没有修复所有警告的情况下进行编译和工作,忽视它们也是一个坏习惯。事实上,如果可以的话,你应该启用更多的警告,并且也要注意这些警告。