2014-11-06 102 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` 

  • 以在一個時間問題之一是爲了避免被所有的人一下子不知所措的好方法。從一開始就一次只拿一個。通常修復之前報告的一個問題會導致其他警告和錯誤消失,因此您不必爲此煩惱。

  • 不要忽視警告。即使你的程序可以在沒有修復所有警告的情況下進行編譯和工作,忽視它們也是一個壞習慣。事實上,如果可以的話,你應該啓用更多的警告,並且也要注意這些警告。