2012-07-12 131 views
0

我试图做一个简单的文本加密(不是所有的功能都完成),但我有一个特定的问题,我想要一些帮助。我停止在编码的这一点,并添加了一些评论文档进行测试: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 
} 

没有任何语法错误,只是逻辑错误。

+4

这并不完全清楚是什么问题。请你可以构建一个更简单的程序,专注于核心问题? (即去掉所有与你的问题无关的东西。) – 2012-07-12 11:02:12

+1

'sizeof(ck)/ 4' ...我对此非常怀疑。你不能仅仅假设'sizeof(std :: string)== 4' ... – quasiverse 2012-07-12 11:02:21

+0

由于#defining关键字'true'和'false',你的程序会调用未定义的行为,所以任何结果都是可能的。 – 2012-07-12 11:06:50

回答

1

string substr(size_t pos = 0,size_t n = npos)const;

Generate substring 

Returns a string object with its contents initialized to a substring of the 
current object. 

This substring is the character sequence that starts at character position 
pos and has a length of n characters. 

你的支票必须是:

PASSWORD.substr(x,1)==ck[y] /* instead of PASSWORD.substr(x,x+1) */ 
+0

Thankyou非常哈哈现在一些早晨喝咖啡 – 2012-07-12 11:24:38

+1

我已经失去了很多次我已经得到一个错误。有些日子我真的想要不同类型的序数和红衣主教。 – molbdnilo 2012-07-12 12:31:56