我試圖做一個簡單的文本加密(不是所有的功能都完成),但我有一個特定的問題,我想要一些幫助。我停止在編碼的這一點,並添加了一些評論文檔進行測試:std :: string :: append(std :: string)錯誤的輸出
函數算法(): 行16-25將std :: string ck []的索引附加到std :: string KEY裏面嵌套循環。 外部的循環針對PASSWORD.size()的度量運行,並針對所有ck []檢查每個PASSWORD.substr(...)。
然而,輸出結果只是PASSWORD的第一個和最後一個字符的索引值。例如。如果PASSWORD = abc KEY輸出0002.我希望整個密碼變成KEY(如口述)。
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
std::string PASSWORD, KEY, ENCRYPTED_TEXT;
void algorithm(std::string note){
ENCRYPTED_TEXT.append(note);
/**STANDARD RULESET**/
std::string ck[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
for(int x=0;x<PASSWORD.size();x++){
for(int y=0;y<sizeof(ck)/4;y++){
if(PASSWORD.substr(x,x+1)==ck[y]){
std::stringstream ind;
if(y>9) ind << y+1;
else ind << "0" << y;
KEY.append(ind.str());
}
}
}
std::cout << "\nKey is " << KEY;
//make_file(ENCRYPTED_TEXT);
}
void qn_setup(){
bool ask=true;
while(ask=true){
std::string check1="", check2="";
std::cout << "Type a password:\n";
std::cin >> check1;
std::cout << "Confirm password:\n";
std::cin >> check2;
if(check1==check2) {
PASSWORD=check1;
ENCRYPTED_TEXT=check1;
ask=false;
break; }
else { std::cout << "\nPasswords did not match.\n"; }
}
}
int main(){
qn_setup();
algorithm(""); //testing key
}
沒有任何語法錯誤,只是邏輯錯誤。
這並不完全清楚是什麼問題。請你可以構建一個更簡單的程序,專注於核心問題? (即去掉所有與你的問題無關的東西。) – 2012-07-12 11:02:12
'sizeof(ck)/ 4' ...我對此非常懷疑。你不能僅僅假設'sizeof(std :: string)== 4' ... – quasiverse 2012-07-12 11:02:21
由於#defining關鍵字'true'和'false',你的程序會調用未定義的行爲,所以任何結果都是可能的。 – 2012-07-12 11:06:50